1 / 7

Pairing elements in two lists

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)).

ollie
Télécharger la présentation

Pairing elements in two lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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? (defun pair_elements (A B) ) (cond ( (or (null A) (null B) ) ‘() ) ( t ) (cons ) (list ) (first A) (first B) (pair_elements (rest A) (rest B))

  2. Announcement No lecture this Thursday 5th Feb.

  3. Taking the pairs apart again >(separate_elements ‘((jack jill) (bonnie clyde) (romeo juliet)) ) ‘((jack bonnie romeo) (jill clyde juliet)) stopping? (null L) recursive? (separate_elements (rest L) ) These are easy. The challenge is putting the “left-over” elements onto the answer returned by the recursive call. What should the recursive call return? If L is ‘( (jack jill) (bonnie clyde) (romeo juliet) ), then > (separate_elements (rest L)) returns (setf Ans ) ‘( (bonnie romeo) (clyde juliet) ) What do we want do do to this Ans to get the full answer? (list ) (cons ‘jack ) (caar L) (car Ans) (cons ‘jill ) (cadar L) (cadr Ans) This is just a step on the way, though!

  4. Where do we hold a temp answer? (Defun separate_elements(L) (cond ( (null L) ‘( () () ) ) ( t ) )) (setf Ans (separate_elements (rest L))) (list ) (cons (caar L) (car Ans)) (cons (cadar L) (cadr Ans)) 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.

  5. List containing (variable-name value) pair List of lists containing all such pairs (in this example there is only one such pair) We use a new construct Let to hold temporary variable values. (Defun separate_elements(L) (cond ( (null L) ‘( () () ) ) ( t ) )) (let ) ( ) ( ) Ans (separate_elements (rest L)) (list ) (cons (caar L) (car Ans)) (cons (cadar L) (cadr Ans)) The “Temporary variable binding” that the let does will only hold within the “scope” of this let statement. 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.

More Related