Download presentation
Presentation is loading. Please wait.
1
(cons ) (defun pair_elements (A B) ) (cond ( (or (null A) (null B) ) ‘() ) ( t ) (list ) Pairing elements in two lists >(pair_elements ‘(jack bonnie romeo) ‘(jill clyde juliet)) ( (jack jill) (bonnie clyde) (romeo juliet) ) (pair_elements A B) stopping condition? (careful!) Suppose we stop when either A or B is empty: (or (null A) (null B)) We want to go through both lists at the same time; Recursive call? (pair_elements (rest A) (rest B)) There are two missing parts; what do we do with them? (first A)(first B) (pair_elements (rest A) (rest B))
2
Announcement No lecture this Thursday 5 th Feb.
3
(setf Ans ) > (separate_elements (rest L)) returns Taking the pairs apart again >(separate_elements ‘((jack jill) (bonnie clyde) (romeo juliet)) ) ‘((jack bonnie romeo) (jill clyde juliet)) stopping? These are easy. The challenge is putting the “left-over” elements onto the answer returned by the recursive call. (null L)(separate_elements (rest L) )recursive? What should the recursive call return? ‘( (bonnie romeo) (clyde juliet) ) What do we want do do to this Ans to get the full answer? If L is ‘( (jack jill) (bonnie clyde) (romeo juliet) ), then (cons ‘jack ) (car Ans)(cons ‘jill ) (cadr Ans) (caar L) (cadar L)(list ) This is just a step on the way, though!
4
(list ) Where do we hold a temp answer? (Defun separate_elements(L) (cond ( (null L) ‘( () () ) ) ( t ) )) But this isn’t good; what if there already is a variable Ans defined by some other function? This will change its value. (Defun separate_elements(L) (cond ( (null L) ‘( () () ) ) ( t (list (cons (caar L) (car (separate_elements (rest L)) )) (cons (cadar L) (cadr (separate_elements (rest L)) )) ) ) ) )) This is not efficient; we end up working out the answer for the recursive call (separate_elements (rest L)) two times. (setf Ans (separate_elements (rest L))) (cons (caar L) (car Ans)) (cons (cadar L) (cadr Ans))
5
List containing (variable-name value) pairList of lists containing all such pairs (in this example there is only one such pair) (list ) We use a new construct Let to hold temporary variable values. (Defun separate_elements(L) (cond ( (null L) ‘( () () ) ) ( t ) )) The “Temporary variable binding” that the let does will only hold within the “scope” of this let statement. (let ) (cons (caar L) (car Ans)) (cons (cadar L) (cadr Ans)) Ans(separate_elements (rest L))( ) This is the best solution to cases where we want to make multiple changes to a recursively-obtained answer (as in separate_elements, where we want to add things to both lists in the answer at the same time). ( )
6
Syntax of let A let statement starts with the word let followed by a list of lists (pairs); each pair giving a variable name and its value. After this list of pairs is the body of the let. The let statement evaluates to the last statement in the body. The variable/value bindings hold in this body and cease once it ends. ( let ( ( a (* 3 3) ) ( b (* 4 4) ) ) (sqrt (+ a b)) ) In let you can’t refer to other variables in a given variable’s definition; you couldn’t have (c (+ a b) ) as a pair.
7
Let* In let you can’t refer to other variables in a given variable’s definition; you couldn’t have (c (+ a b) ) as a pair. If you want to do that, you need let* ( let* ( ( a (* 3 3) ) ( b (* 4 4) ) ( c (+ a b) ) ) (sqrt c) ) Let* evaluates the values of the variables in sequential order from the start to the end of the list. A variable can only refer to the value of an earlier variable in this list.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.