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

CSE240 (online) Lecture 13

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

CSE240 (online) Lecture 13

Introduction to Programming Languages
Programming with LISP (Control Structures and Data Structures)
(201805)

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. CSE240 – Introduction to Programming Languages (online) Lecture 13: Programming

    with LISP| Control Structures, Function-Making, and Data Structures Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    2 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.
  3. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    3 Control Structures • (if (<= 3 2) (* 3 9) (+ 4 2 3)) ; if (3<=2) {return 3*9;} else {return 4+2+3;} 9 • (if (> 2 3) 9) ; if (2>3) {return 9;} else {return nil} NIL • (if (= 2 2) (if (> 3 2) 4 6) 9) 4 • (+ 4 (if (= 2 2) (* 9 2) 7) ) 22
  4. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    4 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
  5. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    5 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
  6. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    6 Global Variables • Variables are set with the macro setf. • (setf x (* 3 2)) 6 • x 6 • (setf y (+ x 3)) 9
  7. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    7 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
  8. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    8 Nested Variable Scope • (let ((x 3) (y (+ 4 9))) (* x y)) 39 • (let ((x 3)) (print x) (let (x) (print x) (let ((x "hello")) (print x)) (print x)) (print x) (print ”end")) 3 NIL "hello" NIL 3 "end" "end"
  9. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    9 Function Making • functions are created by calling a function-making macro. This macro is called defun. • the function returns the value of the last expression. (defun function-name-symbol (param1 param2 param3 ...) expr1 expr2 expr3 ... )
  10. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    10 Function Making • (defun do-hello-world ( ) "Hello, World!") DO-HELLO-WORLD • (do-hello-world) "Hello, World!" • (defun add-four (x) (+ x 4)) ADD-FOUR • (add-four 7) 11 • (defun hypotenuse (length width) (sqrt (+ (* length length)(* width width)))) HYPOTENUSE • (hypotenuse 7 9) 11.4017
  11. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    11 Function Making • (defun first-n-chars (string n reverse-iT) (if reverse-iT (subseq (reverse string) 0 n) (subseq string 0 n))) FIRST-N-CHARS • (first-n-chars "hello world" 5 nil) "hello" • (first-n-chars "hello world" 5 t) "dlrow" • (first-n-chars "hello world" 5 2013) "dlrow"
  12. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    12 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))
  13. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    13 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.
  14. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    14 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 ( last ) ) C
  15. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    15 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)) ;( last ( first (L)) Error: Cannot take CDR of A. • (car (cdr myList2)) ;( first ( last (L)) (B NIL (C NIL NIL))
  16. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    16 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 …)
  17. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    17 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)
  18. CSE240 – Introduction to Programming Languages (online) Javier Gonzalez-Sanchez [email protected]

    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.