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.
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))
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)
Example 3 ; defines a function (get N L) that returns the N member ; of list L –assuming that the elements are numbered from ; zero onwards (defun get (N L) (if (null L) nil (if (zerop N) (first L) (get (1- N) (rest L)) ) ) )
Questions 1. Create a function that receives a list and return the lowerst value 2. Create a function that receives a list and an integer and return “true” if the number exist in the list 3. Create a function that receives a list and return “true” if the list contains a palindrome. such as (a b c d e d c b a) 4. Create a variable in Lisp to store a tic-tac-toe, such as x E x 0 X 0 E x E
Questions 5. Create a function that receives the variable with the tic-tac-toe and return “true” if there is an empty space (an “E” letter). 6. The following list represent a tree '(a (b () ()) (c (d () () ) (e () ()) ) ) Create a function (recursive) that traverse the tree and print all the elements as A B C D E. 7. What is printed by the following code? (setf route '(boston cambridge lincoln concord)) (print (first (rest route)))
Questions 8. What is the output of the following code? (let ((x '(1 2)) (y '(9 10))) (print (+ (first x) (first y)) ) ) 9. What is printed by the following code? (defun fun(n) (if (= n 0) () (fun (- n 1)) ) n ) (print (fun 5)) 10. What is equivalent code in C++ for the following LISP statement (if (< 1 2) (+ 3 4) (- 5 6) )
Solve 5 of the following problems 1. A function test-min that returns the lower value in a List of numbers (test-min L) 2. A function test-average that returns the average of all values in a List of numbers (test-average L) 3. A function test-fibonacci that prints the first n elements of the fibonacci series (test-fibonacci n) 4. A recursive function that returns the leftmost even element in a list of numbers (test-even L) 5. A functions that traverse a list and returns how many of its elements are 0 (test-count-zeros L) 6. A recursive function that return the N member of a list L (assuming that the elements are numbered from zero onwards): (test-get n L) 7. A recursive function that returns non-NIL if x is a member of the list L. (test-is-in-list x L)