Fundamentals of CS I (CS151 2002F)

**Primary:**
[Skip To Body]
[Front Door]
[Current]
[Glance]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]

**Groupings:**
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]

**ECA:**
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]

**Miscellaenous:**
[Scheme Reference]
[CS151 2002F Gum]
[CS151 2001S]
[SamR]
[Glimmer Labs]
[schemers.org]

Back to Vectors, Continued. On to Local Bindings, Continued.

**Held** Tuesday, October 8, 2002

**Summary**

Today we consider how to bind names to values using Scheme's `let`

expressions.

**Today's Work**

**Notes**

- Are there any further questions on exam 1?
- The topic of the week after break will be graphics with Scheme. Would
you prefer
- DrScheme's built-in graphics procedures?
- The Gimp, Linux's photoshop alternative (bitmap graphics)?
- Vector graphics with PostScript?

- The trustees voted this past weekend to demolish Darby after the 2003-2004 Basketball and Volleyball seasons. Sorry.
- Information about the Strategic Planning Committee is available at
`http://www.grinnell.edu/offices/institutionalplanning/strategicplanning/`

- Particularly interesting is the 90+ page overview of Grinnell (in the documents for the first meeting).

**Overview**

- Why name things
- Naming things with
`let`

- Example

- Naming things with
`let*`

- Naming procedures

- As we've seen in many problems, it helps to name the values
that we use within our procedure. Why?
- It can make the code more readable because the name tells us something about the role the value plays.
- It can make the code more efficient, because it allows us to avoid recomputing a value.

- Consider the inefficient but elegant
`closest-to-zero`

(define closest-to-zero (lambda (lst) (cond ; If there's only one element in the list, it's closest to zero ((null? (cdr lst)) (car lst)) ; If the current element is closer to zero than the closest ; remaining thing, use that ((< (abs (car lst)) (abs (closest-to-zero (cdr lst)))) (car lst)) ; Otherwise, use the thing in the remainder closest to zero (else (closest-to-zero (cdr lst))))))

- Instead of making two calls to
`closest-to-zero`

, we can make one by naming the result and using it twice. One possibility is to use a helper procedure(define closest-to-zero (lambda (lst) (cond ((null? (cdr lst)) (car lst)) (closer-to-zero (car lst) (closest-to-zero (cdr lst)))))) (define closer-to-zero (lambda (guess1 guess2) (if (< (abs guess1) (abs guess2)) guess1 guess2)))

- Another possiblity is to
*name*the result of the recursive computation.(define closest-to-zero (lambda (lst) (cond ((null? (cdr lst)) (car lst))

*"Compute (closest-to-zero (cdr lst)) and call it guess"*(if (< (abs (car lst)) (abs guess)) (car lst) guess)))) - Another reasons to name things is that we might want to create helper procedures and only make them available to the current procedure.

`let`

- You name things with
`let`

. -
`let`

has the form(let ((

*name*_{1}*exp*) (_{1}*name*_{2}*exp*) ... (_{2}*name*_{n}*exp*))_{n}*body*) -
`let`

has the meaning:- Evaluate all the expressions.
- Update the binding table to associate each name with the corresponding value.
- Evaluate
*body*using the updated binding table. - Eliminate all the bindings just created.

- You can use
`let`

in a simple expression:(define values (list 1 4 2 4 1 5 9)) (let ((largest (max values)) (smallest (min values))) (/ (+ largest smallest) 2))

- More frequently, we use
`let`

within a procedure. Here's a new version of`closest-to-zero`

that uses`let`

.(define closest-to-zero (lambda (lst) ; If there's only one element in the list, it's closest to zero (if (null? (cdr lst)) (car lst) ; Otherwise, find the remaining element closest to zero and ; call it guess (let ((guess (closest-to-zero (cdr lst)))) ; Choose the closer to zero of the first element and guess (if (< (abs (car lst)) (abs guess)) (car lst) guess)))))

`let*`

- If we want to bind some things in sequence, we need to use
`let*`

rather than`let`

. -
`let*`

has the form(let* ((

*name*_{1}*exp*) (_{1}*name*_{2}*exp*) ... (_{2}*name*_{n}*exp*))_{n}*body*) -
`let*`

has the meaning:- Evaluate
*exp*._{1} - Update the binding table to associate
*name*with that value._{1} - Evaluate
*exp*._{2} - Update the binding table to associate
*name*with that value._{2} - ...
- Evaluate
*exp*._{n} - Update the binding table to associate
*name*with that value._{n} - Evaluate
*body*using the updated binding table. - Eliminate all the bindings just created.

- Evaluate

- You can also use this technique to name helper procedures. However, it does not work for recursive helper procedures.
- We'll return to recursive helper procedures next week.
- Here's an example of the use of a non-recursive helper procedure that checks
whether a value of any type is exact
;;; Procedure: ;;; exact-average ;;; Parameters: ;;; num1, an exact number ;;; num2, an exact number ;;; Purpose: ;;; Average the two numbers. ;;; Produces: ;;; average, an exact number ;;; Preconditions: ;;; num1 is an exact number [Verified] ;;; num2 is an exact number [Verified] ;;; Postconditions: ;;; Guess. (define exact-average (lambda (num1 num2)

**(let ((verify? (lambda (val) (and (number? val) (exact? val)))))**(cond ((not (verify? num1)) (error "exact-average" "first parameter is a non-number")) ((not (verify? num2)) (error "exact-average" "second parameter is a non-number")) (else (/ (+ num1 num2) 2))))))

Thursday, 29 August 2002 [Samuel A. Rebelsky]

- Created, based on the outlines from CSC151 2001S.

Monday, 7 October 2002 [Samuel A. Rebelsky]

- Updated example to use
`closest-to-zero`

. - Other minor changes to text.
- Added introductory notes.

Back to Vectors, Continued. On to Local Bindings, Continued.

**Primary:**
[Skip To Body]
[Front Door]
[Current]
[Glance]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]

**Groupings:**
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]

**ECA:**
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]

**Miscellaenous:**
[Scheme Reference]
[CS151 2002F Gum]
[CS151 2001S]
[SamR]
[Glimmer Labs]
[schemers.org]

**Disclaimer**:
I usually create these pages on the fly

, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.

This document was generated by
Siteweaver on Mon Dec 2 08:41:20 2002.

The source to the document was last modified on Mon Oct 7 22:27:38 2002.

This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Outlines/outline.23.html`

.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu