Iteration Chapters 6 7 Iteration in LISP unlike

  • Slides: 21
Download presentation
Iteration Chapters 6 & 7

Iteration Chapters 6 & 7

Iteration in LISP (unlike Prolog) allows iteration – mapcar, remove-if(-not), count-if, find-if for special

Iteration in LISP (unlike Prolog) allows iteration – mapcar, remove-if(-not), count-if, find-if for special purpose iteration n Dotimes, dolist & do allow more general iteration – often used with side-effects

Review of Do n Do takes list of local variables, stopping condition plus return

Review of Do n Do takes list of local variables, stopping condition plus return expression and body > (do ((i 1 (1+ i))) ((> i 10) ‘done) (format t “~a ” i)) 1 2 3 4 5 6 7 8 9 10 DONE

Do. Times Example n Exponentiation function – (power 5 3) => 125 extra form

Do. Times Example n Exponentiation function – (power 5 3) => 125 extra form in defun just for the side effect (defun power (M N) (setf result 1) (dotimes (i N result) (setf result (* M result)))) loop control variable body of the loop done for the side effect upper bound of the loop return value

Do. Times Operation (dotimes (i N result) <body>) for ( i = 0; i

Do. Times Operation (dotimes (i N result) <body>) for ( i = 0; i < N; i++ ) <body> return result; (dotimes (i N res) (setf res (* M res))) for ( i = 0; i < N; i++) res = M * res; return res; Loop control variable is not evaluated Upper bound is evaluated before 1 st iteration Return result is evaluated after last iteration Body evaluated once per iteration

Do. Times Factorial n Loop control variable available in the loop – just like

Do. Times Factorial n Loop control variable available in the loop – just like loop control in imperative languages (defun factorial (N) (setf fact 1) (dotimes (i N fact) (setf fact (* (1+ i) fact)))) Need to add 1 to i, because i runs from 0 to N– 1, not 1 to N

Exercise n Write a function using dotimes to calculate the sum of the numbers

Exercise n Write a function using dotimes to calculate the sum of the numbers from 1 to N – can you do it without adding 1 to i each time?

Solution (defun sum-to-N (N) (setf sum 0) (dotimes (i (1+ N) sum) (setf sum

Solution (defun sum-to-N (N) (setf sum 0) (dotimes (i (1+ N) sum) (setf sum (+ sum i)))) n Run loop to N+1 so we get sum 0. . N – 0 adds nothing to the sum, so that’s OK n Alternative: (dotimes (i N (+ sum N)) …)

Local vs. Global Variables n Functions above use global variables – not good –

Local vs. Global Variables n Functions above use global variables – not good – what if someone was using them already? – what if one of the functions called uses the same global variable? Would like all the variables to be local n LISP gives us let and let* n

Let n Let defines & initializes local variables > (let ((v 1 10) (v

Let n Let defines & initializes local variables > (let ((v 1 10) (v 2 5)) (+ v 1 v 2)) 15 > v 1 ERROR: undefined variable n (let <list-of-variable-value-pairs> <body>) – value of <body> becomes value of let

Using Let (defun power (M N) (let ((result 1)) (dotimes (i N result) (setf

Using Let (defun power (M N) (let ((result 1)) (dotimes (i N result) (setf result (* M result)))) result now local – no complications n Note: ((result 1)) n – list of variable-value pairs – each pair in its own list

Let with Multiple Variables n Can use as many as you like (defun correlation

Let with Multiple Variables n Can use as many as you like (defun correlation (X Y) (let ((N (length X)) (Sx (sum-list X)) (Sy (sum-list Y)) (Sxy (dot-product X Y)) (Sxx (sum-of-squares X)) (Syy (sum-of-squares Y))) (/ (– (* N Sxy) (* Sx Sy)) (* …)))))

Exercise n Rewrite factorial and sum-to-N using lets (defun factorial (N) (setf fact 1)

Exercise n Rewrite factorial and sum-to-N using lets (defun factorial (N) (setf fact 1) (dotimes (i N fact) (setf fact (* (+ i 1) fact)))) (defun sum-to-N (N) (setf sum 0) (dotimes (i (+ N 1) sum) (setf sum (+ sum i))))

Let Works in Parallel n All assignments are done “at the same time” –

Let Works in Parallel n All assignments are done “at the same time” – can’t use the value of one to set another > (let ((x 5) (y (+ x 10))) …) ERROR – unbound variable X n Won’t get an error if x is defined – you’ll just get the other value of x – (let ((x 5) (y (+ x 10))) (+ x y)) => 796 (!!!) “other” X was 781

Let* Works in Serial n In case you want to set one value based

Let* Works in Serial n In case you want to set one value based on another > (let* ((x 5) (y (+ x 10))) (+ x y)) 20 n X is already a local variable by the time let* gets to it

Do. List n Do. List allows iteration over lists – do something for each

Do. List n Do. List allows iteration over lists – do something for each element of a list – also usually with side-effects n (dolist (<var> <list> <result>) <body>) – <var> set to each element of <list> in turn – <body> evaluated for each value – <result> evaluated & returned

Do. List Example n (do-reverse ‘(1 2 3 4 5)) => (5 4 3

Do. List Example n (do-reverse ‘(1 2 3 4 5)) => (5 4 3 2 1) (defun do-reverse (L) (let ((rev ())) (dolist (e L rev) (setf rev (cons e rev))))) n Elements added to front of rev – rev = (), (cons 1 rev) => (1) – rev = (1), (cons 2 rev) => (2 1)

Sum-List Using Do. List > (defun sum-list (L) (let ((sum 0)) (dolist (e L

Sum-List Using Do. List > (defun sum-list (L) (let ((sum 0)) (dolist (e L sum) (setf sum (+ sum e))))) > (sum-list ‘(4 7 15)) sum 0 e 4, sum (+ 0 4) => 4 e 7, sum (+ 4 7) => 11 e 15, sum (+ 11 15) => 26 => sum => 26

Exercise n Write the function do-intersect, to calculate the intersection of two lists using

Exercise n Write the function do-intersect, to calculate the intersection of two lists using let and dolist > (do-intersect ‘(1 3 5 7 9) ‘(3 6 9 12)) (9 3)

Solution n Iterate thru first list – if current element is in second list,

Solution n Iterate thru first list – if current element is in second list, add it to the intersection – intersection should have started as empty (defun do-intersection (L 1 L 2) (let ((inter ())) (dolist (e L 1 inter) (when (member e L 2) (push e inter)))))

Next Time n Applications – chapters 24 & 26

Next Time n Applications – chapters 24 & 26