1.2k

# CSE240 Lecture 23

Introduction to Programming Languages
Functions in Lisp
(202204)

January 23, 2017

## Transcript

1. ### jgs CSE 240 Introduction to Programming Languages Lecture 23: Functions

in Lisp Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

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

5. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 5 jgs

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

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

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"

9. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 9 jgs

Test Yourselves 1 public static double factorial (double n) { double sum = 1; for (double i=0;i < n; i++) sum = sum * (1 + i); return sum; } public static void main(String [] args) { System.out.println(factorial(1000)+""); }
10. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 10 jgs

Answer 1 (defun factorial (n) (let ((sum 1)) (dotimes (i n) (setf sum (* sum (1+ i))) ) sum ) ) FACTORIAL (factorial 1000) result? … double factorial (double n) { double sum = 1; for (double i=0;i < n; i++) sum = sum * (1 + i); return sum; } result?
11. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 11 jgs

Test Yourselves 2 public static double factorial (double n) { if (n<=1) return 1; else return n * factorial (n-1); } public static void main(String [] args) { System.out.println(factorial(1000)+""); }
12. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 12 jgs

Answer 2 (defun factorial (n) (if (<= n 0) 1 (* n (factorial (- n 1))) ) ) FACTORIAL (factorial 1000) result? … double factorial (double n) { if (n<=1) return 1; else return n * factorial (n-1); } result?
13. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 13 jgs

Announcement § Homework 05 and Quiz 05 are now open. § They are due on November 30. § They are all about LISP

15. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 15 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))
16. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 16 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.
17. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 17 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
18. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 18 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))
19. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 19 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
20. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 20 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)
21. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 21 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)

23. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 23 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)) ) ) )
24. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 24 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))) ) )
25. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 25 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)) ) ) )

27. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 27 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
28. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 28 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)))
29. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 29 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) )
30. ### Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 30 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)

Questions
32. ### jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez, Ph.D.

javiergs@asu.edu 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.