# Naming Values with Local Bindings

## Exercises

### Exercise 0: Preparation

If you haven't done so already (or if you had problem understanding the short lecture), you may want to scan through the reading on local bindings.

### Exercise 1: Evaluating `let`

What are the values of the following `let`-expressions? You may use DrScheme to help you answer these questions, but be sure you can explain how it arrived at its answers.

a.

```(let ((tone "fa")
(call-me "al"))
(list call-me tone "l" tone))
```

b.

```;; Solutions to the quadratic equation x^2 - 5x + 4:
;;
(let ((discriminant (- (* -5 -5) (* 4 1 4))))
(list (/ (+ (- -5) (sqrt discriminant)) (* 2 1))
(/ (- (- -5) (sqrt discriminant)) (* 2 1))))
```

c.

```(let ((total (+ 8 3 4 2 7)))
(let ((mean (/ total 5)))
(* mean mean)))
```

d.

```(let ((val (read)))
(display "The square of ")
(display val)
(display " is ")
(display (* val val))
(newline))
```

### Exercise 2: Matching Likes

Suppose we are recording student information for Vivek's Dating Service in the following form:

```(first-name last-name gender list-of-likes)
```

Write a procedure `(likes thing list-of-students)` that extract all the students that like a particular thing. Use `let` to simplify the process.

### Exercise 3: Nesting Lets

Write a nested `let`-expression that binds a total of five names, `a`, `b`, `c`, `d`, and `e`, with `a` bound to 9387 and each subsequent name bound to a value twice as large as the one before it -- `b` should be twice as large as `a`, `c` twice as large as `b`, and so on. The body of the innermost `let`-expression should compute the sum of the values of the five names.

### Exercise 4: Simplifying Nested Lets

Write a `let*`-expression equivalent to the `let`-expression in the previous exercise.

### Exercise 5: Finding the Longest List

Here is a procedure that takes a non-empty list of lists as argument and returns the longest list on the list (or one of the longest lists, if there is a tie).

```(define longest-list-in-list
(lambda (ls)
(if (null? (cdr ls))
(car ls)
(longer-list (car ls) (longest-list-in-list (cdr ls))))))
```

This definition of the `longest-string-in-list` procedure includes a call to the `longer-string` procedure, which returns the longer of two given strings:

```(define longer-string
(lambda (left right)
(if (<= (string-length right) (string-length left))
left
right)))
```

Revise the definition of `longest-list-in-list` so that the name `longer-list` is bound to the procedure that it denotes only locally, in a `let`-expression.

### Exercise 6: Alternate Techniques

Note that there are two possible ways to do the previous exercise: The definiens of `longest-list-in-list` can be a `lambda`-expression with a `let`-expression as its body, or it can be a `let`-expression with a `lambda`-expression as its body.

a. Define `longest-list-in-list` in whichever way that you did not define it for the previous exercise.

b. Does the order of nesting affect what happens when the procedure is invoked? If so, which arrangement is better? Why?

## History

Monday, 2 October 2000

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.