Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

jgs Previously …

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 3 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

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 4 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

Slide 5

Slide 5 text

jgs Loops

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 6 jgs 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 • (dotimes (x 4 100) (print "hello")) "hello" "hello" "hello" "hello" 100

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 7 jgs Loop (dotimes) dotimes is an iterator with this format (dotimes (var high-val optional-return-val) expr1 expr2 …) • (setf bag 2) 2 • (dotimes (x 3) (setf bag (* bag bag))) NIL • (print bag) 256

Slide 8

Slide 8 text

jgs Variables

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 9 jgs Global Variables § Variables are set with the macro setf. • (setf x (* 3 2)) 6 • x 6 • (setf y (+ x 3)) 9

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 11 jgs 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"

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 12 jgs Questions

Slide 13

Slide 13 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.