1 / 7

# Pairing elements in two lists - PowerPoint PPT Presentation

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

### Download Presentation

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

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

### Announcement

No lecture this Thursday 5th Feb.

### 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!

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

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

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

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