Everything is a List § (+ 27/32 32/57) 2563/1824 § (* 2.342 3.2e4) 74944.0 § (* 2.342 9.212 -9.23 3/4) ; You can mix number types -149.34949 § (/ 3 5) ; The return type stays as general as possible 3/5 § (/ 3.0 5) ;Here LISP had no choice: convert to a float 0.6 § (1+ 3) 4
Everything is a List § (string-upcase "How about that!") "HOW ABOUT THAT!" § (reverse "Four score and seven years ago") "oga sraey neves dna erocs ruoF" § (length "Four score and seven years ago") 30 § (sqrt 2) 1.4142135 § (sqrt -1.0) #C(0 1.0) § (SqRt -1.0) ;LISP symbols are case-insensitive #C(0 1.0)
Function Arguments § Some functions have NOT a fixed number of arguments: (+ 100 231 201 921 221 231 -23 12 -34 134) § Some functions have a fixed number of arguments and optional arguments: (subseq "Four score and seven years ago" 9) "e and seven years ago” (subseq "Four score and seven years ago" 9 23) "e and seven ye"
Predicates (boolean functions) § LISP has a special name for functions which return "true" (usually t) or "false" (nil). These functions are called predicates. • (= 4 3) ; is 4 == 3 ? NIL • (< 3 9) ; is 3 < 9 ? T • (numberp "hello") ; is "foo" a number? NIL • (oddp 9) ; is 9 an odd number? T
Errors § Errors stop the program execution • (/ 1 0) *** - division by zero • (blah-blah-blah 1 0 "foo") *** - EVAL: the function BLAH-BLAH-BLAH is undefined
Control Structures § There are some evaluable lists which are not functions. § These lists are known as macros or special forms. § The control structure if is a special form.
Control Structures § if only allows one test-expression, one then-expression, and one optional- else-expression. § To do more things in the then-expression we need to make a block. Blocks are made with the special form progn, which takes the form: (progn expr1 expr2 expr3 ...) • (if (> 3 2) (progn (print "hello") (print "yo") (print "whassup?") (+ 2 2)) (+ 1 2 3)) "hello" "yo" "whatsup?" 4
[email protected] Fall 2021 Copyright. These slides can only be used as study material for the class CSE240 at Arizona State University. They cannot be distributed or used for another purpose.