# Notes on Lab 1: Scheme

You are not required to turn in anything for the lab. However, you are expected to consider as many of these questions as you can. You are encouraged to work with other students. Those who survived the Scheme-based introduction are encouraged to act as TAs.

Use `map` to square all the elements of some sequence.

```> (map (lambda (x) (* x x)) '(1 2 3 4 5))
(1 4 9 16 25)
```

Use `map` to increment all the elements of some sequence.

```> (map (lambda (x) (+ x 1)) '(1 2 3 4 5))
(2 3 4 5 6)
```

Write a function, `makepairs` that, given two lists, makes the list of pairs of elements from those two lists. For example `(makepairs '(1 2 3) '(a b c))` should create the list `((1 a) (2 b) (3 c))`. You should write `makepairs` recursive. Do not use `map` to write this function.

This is interesting in that we must make a list of lists. As many of you saw, some care is required in constructing both lists and lists of lists. Some consideration of what to do when the lists are of unequal length is also necessary. I've chosen to stop when either list runs out of elements.

```> (define (makepairs l1 l2)
(if (or (null? l1) (null? l2)) '()
(cons (list (car l1) (car l2))
(makepairs (cdr l1) (cdr l2)))))
> (makepairs '(1 2 3) '(a b c))
((1 a) (2 b) (3 c))
> (makepairs '(1 2 3) '(a b))
((1 a) (2 b))
> (makepairs '(1 2) '(a b c))
((1 a) (2 b))
```

Rewrite `makepairs` without recursive calls, using `map`.

You may have observed that all we're doing is ``do something to the first element of each list and then recurse on the rest''. That sounds like a candidate for `map`. What are we doing? Just listing the first two elements.

```> (define (makepairs l1 l2)
(map (lambda (x y) (list x y)) l1 l2))
> (makepairs '(1 2 3) '(a b c))
((1 a) (2 b) (3 c))
> (makepairs '(1 2 3) '(a b))

Error in map: lists (1 2 3) and (a b) differ in length.
Type (debug) to enter the debugger.
```

Of course, the anonymous function we're using is basically identical to `list`, so we can just use `list`.

```> (define (makepairs l1 l2)
(map list l1 l2))
> (makepairs '(1 2 3) '(a b c))
((1 a) (2 b) (3 c))
```

Write a function, `mappairs`, that applies a binary function to each pair in a list of pairs, creating a list of results. For example, `(mappairs * '((1 2) (2 3) (3 4)))` should give `(2 6 12)`.

Once again, it makes sense to use `map`.

```> (define (mappairs fun pairs)
(map (lambda (pair) (fun (car pair) (cadr pair))) pairs))
> (mappairs * '((1 2) (2 3) (3 4)))
```

Write a function, `sum`, that sums the elements in a list. For example, `(sum '(1 2 3))` should give 6.

There's probably an appropriate Scheme function to insert a binary operator between any two elements of a list. However, I don't know it, so I'll just write this recursively.

```> (define (sum l)
(if (null? l) 0
(+ (car l) (sum (cdr l)))))
> (sum '(1 2 3))
6
> (sum '(3 4 5))
12
> (sum '(0 -1 2))
1
```

Using all of these functions, write `innerProduct` that computes the inner product of two vectors.

The inner product of two vectors is the sum of the squares of the corresponding elements. We can write this as

```> (define (innerprod v1 v2)
(sum (mappairs * (makepairs v1 v2))))
> (innerprod '(1 2 3) '(1 2 3))
14
> (innerprod '(10 3 2) '(34 1 4))
351
> (innerprod '() '())
0
```

Think about how you'd write this in an imperative langauge and which is better.

History

• Created Friday, February 5, 1999.

Disclaimer Often, these pages were created ``on the fly'' with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.