BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
201801 CSE240 Lecture 20
1. CSE240 – Introduction to
Programming Languages
Lecture 20:
Programming with LISP| Data Structures
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
2. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2
Data Structures
The only data structure is List
• (quote (hello world 1 2 3))
(HELLO WORLD 1 2 3)
• '(hello world 1 2 3)
(HELLO WORLD 1 2 3)
• (quote (what is (going on) here?))
(WHAT IS (GOING ON) HERE?)
• (quote my-symbol)
MY-SYMBOL
• (quote (+ 4 (* 3 2 9)))
(+ 4 (* 3 2 9))
3. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3
Data Structures
• first returns the first item in a list. The old name of first is car (content of
the address).
• rest returns a list consisting of everything but the first item. It does not
damage the original list. The old name of rest is cdr (content of the
decrement register).
• append hooks multiple lists together.
• cons takes an item and a list, and returns a new list consisting of the
old list with the item tacked on the front.
4. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4
Data Structures
• ( setf myList '( (A B) C (D) ) )
((A B) C (D))
• (car (car myList) ) ;( first ( first ) )
A
• (cdr( car myList)) ;( last ( first ) )
(B)
• (car (cdr myList)) ;( first ( rest ) )
C
5. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5
Data Structures
• ( setf myList2 '(A (B () (C () () )) (D (E () () ) () ) ) )
(A (B NIL (C NIL NIL)) (D (E NIL NIL) NIL))
• (car (car myList2)) ;( first ( first (L))
Error: Cannot take CAR of A.
• (cdr( car myList2)) ;( rest ( first (L))
Error: Cannot take CDR of A.
• (car (cdr myList2)) ;( first ( rest (L))
(B NIL (C NIL NIL))
6. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6
Loop (dolist)
• (dolist (x '(a b c d e)) (print x))
A
B
C
D
E
NIL
dolist is an iterator with this format
(dolist (var list-to-iterate-over optional-return-val)
expr1
expr2
…)
7. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7
Loop (dolist)
• (defun my-reverse (list)
(let (new-list) ; empty list
(dolist (x list)
(setf new-list (cons x new-list))
)
new-list)
)
MY-REVERSE
• (my-reverse '(a b c d e f g))
(G F E D C B A)
8. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8
Loop (dolist)
• (defun my-reverse (list)
(let (new-list) ; empty list
(dolist (x list)
(setf new-list (cons x new-list))
)
new-list)
)
MY-REVERSE
• (my-reverse '(a b c d e f g))
(G F E D C B A)
• (print (first (my-reverse '(a b c d e f g))))
G
• (print (last (my-reverse '(a b c d e f g))))
(A)
9. 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.