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

CSE240 Lecture 21

CSE240 Lecture 21

Introduction to Programming Languages
Lists in LISP
(202011)

Javier Gonzalez-Sanchez
PRO

January 21, 2017
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSE 240
    Introduction to Programming Languages
    Lecture 21: Lists in LISP
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu | javiergs.com
    PERALTA 230U
    Office Hours: By appointment

    View Slide

  2. jgs
    Midterm Exams

    View Slide

  3. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 3
    jgs
    Part 1 / 13
    1. Spragg, Hunter
    2. Vaidya, Yogesh
    3. Donta, Nipoon Naresh
    4. Li, Zhuoran
    5. Vega, Moses

    View Slide

  4. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 4
    jgs
    Part 2 / 13
    1. Seo, Jinha
    2. Wallace, Dallin
    3. Esan, Oluwamayowa
    4. Karimi, Mohammad
    5. Anubrolu, Charishma

    View Slide

  5. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 5
    jgs
    Part 3 / 13
    1. Annison, Madeline
    2. Heinemann, James
    3. Shaw, Ethan
    4. Crandall, Gabriel
    5. Sandy, David

    View Slide

  6. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 6
    jgs
    Part 4 / 13
    1. Modi, Dhruvi Jayeshkumar
    2. NolastName, Maaz Ahmed
    3. Shaikh, Ariyan
    4. Desai, Keyur
    5. Lopez, Maximiliano

    View Slide

  7. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 7
    jgs
    Part 5 / 13
    1. Ozturk, Alper
    2. Osif, Cecil
    3. Thota, Supreeth Yadav
    4. Chen, Shengdong
    5. Meacham, Gabrielle

    View Slide

  8. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 8
    jgs
    Part 6 / 13
    1. Yadav, Rajat
    2. Fishco, Harrison
    3. Alzara, Ibrahim Mohammed I
    4. Jain, Vishesh Nirmal
    5. Buice, Robert

    View Slide

  9. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 9
    jgs
    Part 7 / 13
    1. Sullivan, Nathaniel
    2. Ortega, Gabriel
    3. Vance, Tyler
    4. Archer, Anthony
    5. Baney, Broc

    View Slide

  10. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 10
    jgs
    Part 8 / 13
    1. Carson, Austin
    2. Whitehouse, Chase
    3. Mckinney, Kyle
    4. Robertson, Hannah
    5. Stuva, Brayden

    View Slide

  11. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 11
    jgs
    Part 9 / 13
    1. Braunschweig, Jackson
    2. Cerny, Trenton
    3. Obrien, Sophie
    4. Sanchez, Derek
    5. Bhattacharya, Anand

    View Slide

  12. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 12
    jgs
    Part 10 / 13
    1. Grabowski, Mateusz
    2. Bergeson, Donald
    3. Schwartz, Jeremiah
    4. Masetty, Abhishek
    5. Ummadisingu, Gnana Chaitanya

    View Slide

  13. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 13
    jgs
    Part 11 / 13
    1. Lofgreen, Blake
    2. Perez, Victor Ledezma
    3. Guthy, Pavitra
    4. Lin, Allen
    5. Thompson, Trevor

    View Slide

  14. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 14
    jgs
    Part 12 / 13
    1. Edsitty, Robyn
    2. Kakarla, Hethesh
    3. Mcmahon, Taylor
    4. Gonzalez, Cristian Munoz
    5. Nguyen, Kevin

    View Slide

  15. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 15
    jgs
    Part 13 / 13
    1. Alaklabi, Faisal
    2. Plitzuweit, Zachary
    3. Hagh, David
    4. Chepyala, Alekhya
    5. Sabri, Hamza

    View Slide

  16. jgs
    Previously
    Atomic Expressions

    View Slide

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

    View Slide

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

    View Slide

  19. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 19
    jgs
    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"

    View Slide

  20. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 20
    jgs
    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

    View Slide

  21. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 21
    jgs
    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

    View Slide

  22. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 22
    jgs
    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

    View Slide

  23. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 23
    jgs
    Recursion
    § (and (< 3 (* 2 5))
    (not (>= 2 6))) ; (3 < 2 * 5) && !(2 >= 6)
    T
    § (print (+ 2 3 4 1))
    10
    10
    § (+ (* 2 3) (/ 3 2) 9)
    33/2

    View Slide

  24. jgs
    Control Structures

    View Slide

  25. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 25
    jgs
    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.

    View Slide

  26. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 26
    jgs
    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

    View Slide

  27. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 27
    jgs
    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

    View Slide

  28. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 28
    jgs
    Questions

    View Slide

  29. jgs
    CSE 240 Introduction to Programming Languages
    Javier Gonzalez-Sanchez, Ph.D.
    [email protected]
    Fall 2021
    Copyright. These slides can only be used as study material for the class CSE240 at Arizona State University.
    They cannot be distributed or used for another purpose.

    View Slide