Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE240 Lecture 24

CSE240 Lecture 24

Introduction to Programming Languages
Definition of Functions in LISP
(202011)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

January 24, 2017
Tweet

Transcript

  1. jgs CSE 240 Introduction to Programming Languages Lecture 24: Working

    with Data structures Javier Gonzalez-Sanchez javiergs@asu.edu PERALTA 230U Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 2 jgs

    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 | CSE240 | Spring 2018 | 3 jgs

    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 | CSE240 | Spring 2018 | 4 jgs

    Data Structures § ( setf myList '( (A B) C (D) ) ) ((A B) C (D)) § (car (car myList) ) ;( first ( first ) ) A § (cdr( car myList)) ;( rest ( first ) ) (B) § (car (cdr myList)) ;( first ( rest ) ) C
  5. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 5 jgs

    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 | CSE240 | Spring 2018 | 6 jgs

    Loop (dolist) dolist is an iterator with this format (dolist (var list-to-iterate-over optional-return-val) expr1 expr2 …) § (dolist (x '(a b c d e)) (print x)) A B C D E NIL
  7. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 7 jgs

    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 | CSE240 | Spring 2018 | 8 jgs

    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. jgs More Examples

  10. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 10 jgs

    Example 1 ; A function that return the leftmost even member." (defun find-even (L) (if (null L) nil (if (evenp (first L)) (first L) (find-even (rest L)) ) ) )
  11. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 11 jgs

    Example 2 ; develop recursive functions that traverse a list ; and count its elements (defun recursive-list-length (L) (if (null L) 0 (1+ (recursive-list-length (rest L))) ) )
  12. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 12 jgs

    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)) ) ) )
  13. jgs Test Yourselves

  14. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 14 jgs

    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
  15. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 15 jgs

    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)))
  16. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 16 jgs

    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) )
  17. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 17 jgs

    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)
  18. jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu

    Spring 2018 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.