Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 2 jgs Functional Paradigm

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 3 jgs Semantic Functional Paradigm Key idea: Focus on higher level of abstraction (free from programming details) Features: Simpler semantic: no data types, and a simpler syntax

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 4 jgs Syntax § Uniform representation of instructions and data using a single general format– a list. (functionId param1 param2 param3 ...) • (+ 3 2 7 9) ; add 3+2+7+9 and return the result 21 • (* 4 2.3) ; multiply 4 by 2.3 and return the result 9.2 • (subseq "Hello, World" 2 9) "llo, Wo"

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 5 jgs Functional Programming § Language: we will use LISP (LISt Processing). § It uses an interpreter. § Automatic management of memory.

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 6 jgs You need an Interpreter § LispWork Personal Edition – http://www.lispworks.com/downloads/index.html § Compile Lisp Online – http://rextester.com/l/common_lisp_online_compiler § Steel Bank Common Lisp – http://www.sbcl.org/platform-table.html

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 7 jgs Functional Programming § A program is a set of lists and they are enclosed with parenthesis. § We use lists to represent functions; in a function the first element of the list is the id of the function and the rest are parameter(s) § Whitespace separate function id and parameters § Examples: ( print "Hello World" ) ( + 2 2 ) ( sqrt 2 )

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 8 jgs Important Notice !

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 9 jgs Everything is a List § A function ID (symbol) is a series of characters other than whitespace, parentheses ( ), pound ( # ), quote ( ' ), double-quote ( " ), period ( . ), or back quote ( ` ). § A function ID (symbol) may not take the form of numbers. § It's very common for symbols to have hyphens ( - ) or asterisks ( * ) in them. § There are NOT operators! only functions § Symbols (function ID) are case-INSENSITIVE.

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 10 jgs Atomic Expressions § -3 § 2.43 § 123342303923412323411323234012923492341231230234923410239234412 § #C(3.2 2) ; the complex number 3.2 + 2i § 2/3 ; the fraction 2/3. It is NOT divide 2 by 3 § -3.2e25 ; the number -3.2 x 10^25 § #\g ; characters starts with #\ § #\{ § #\\ ; the character '\' § #\tab
 § #\newline § 
#\space
 § #\backspace
 § #\escape

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 11 jgs Atomic Expressions § "Hello, World!" § "It's a glorious day." § "He said \"No Way!\" and then he left." § "I think I need a backslash here: \\ Ah, that was better." § t ; true § nil ; false

Slide 12

Slide 12 text

jgs Lists

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 13 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

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 14 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)

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 15 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"

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 16 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

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 17 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

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 18 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

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 19 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

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 20 jgs Questions

Slide 21

Slide 21 text

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.