1. CSE240 – Introduction to
Programming Languages
Lecture 17:
Programming with LISP| Control Structures
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
2. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2
Everything is a List
• A function ID (symbol) is a series of characters other than whitespace, parentheses ( ), pound
( # ), quote ( ' ), double-quote ( " ), period ( . ), or back quote ( ` ).
• A function ID (symbol) may not take the form of numbers.
• It's very common for symbols to have hyphens ( - ) or asterisks ( * ) in them.
• There are NOT operators! only functions
• Symbols (function ID) are case-INSENSITIVE.
3. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3
Atomic Expressions
• -3
• 2.43
• 123342303923412323411323234012923492341231230234923410239234412
• #C(3.2 2) ; the complex number 3.2 + 2i
• 2/3 ; the fraction 2/3. It is NOT divide 2 by 3
• -3.2e25 ; the number -3.2 x 10^25
• #g ; characters starts with #
• #{
• # ; the character ''
• #tab
• #newline
• #space
• #backspace
• #escape
4. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4
Atomic Expressions
• "Hello, World!"
• "It's a glorious day."
• "He said "No Way!" and then he left."
• "I think I need a backslash here: Ah, that was better."
• t ; true
• nil ; false
5. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5
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
6. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6
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)
7. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7
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"
8. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8
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
9. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9
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
10. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10
Recursion
• When a list contains another list among its expressions, the evaluation
procedure is recursive.
• (+ 33 (* 2.3 4) 9)
51.2
• (+ (length "Hello World") 44)
55
• (* (+ 3 2.3) (/ 3 (- 9 4))) ;in C++: (3+2.3) * (3 / (9-4))
3.1800003
• (log (log (log 234231232234234123)))
1.3052895
• (+ (* (sin 0.3)
(sin 0.3)) ; expressions may use multiple lines
(* (cos 0.3)
(cos 0.3))) ; sin(0.3)^2 + cos(0.3)^2
1.0000001 ; = 1. Rounding inaccuracy
13. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13
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.
15. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 15
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
16. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 16
Loop (dotimes)
dotimes is an iterator with this format
(dotimes (var high-val optional-return-val)
expr1
expr2
…)
• (dotimes (x 4) (print "hello"))
"hello"
"hello"
"hello"
"hello"
NIL
• (setf bag 2)
2
• (dotimes (x 3) (setf bag (* bag bag)))
NIL
• (print bag)
256
17. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 17
Global Variables
• Variables are set with the macro setf.
• (setf x (* 3 2))
6
• x
6
• (setf y (+ x 3))
9
18. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 18
Local Variables
• let declares local variables with each declaration. Then it evaluates
the expressions in order (as a block).
• let returns the value of the last expression.
• (setf x 4)
(let ((x 3)) ; x declared local
(print x)
(setf x 9) ; change the value of x
(print x)
(+ x x) ;this is the returned value
)
3
9
18
• x ;outside the let, we're back to global again
4
20. CSE240 – Introduction to Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Fall 2017
Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.