Fundamentals of Computer Science I (CS151 2003F)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]

[Honesty]
[Instructions]
[Links]
[Guidelines for Lab Writeups]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[Scheme Report]
[Glimmer Scheme Reference]
[CSC151.01 (Gum)]
[CSC151 2003S]
[CSC151 2002F]
[SamR]
Back to Discussion of Exam 1. On to Local Bindings Laboratory.
Held: Tuesday, 7 October 2003
Summary:
Today we consider how to bind names to values using Scheme's various kinds of let
expressions.
Related Pages:
Due
Notes:
Overview:
let
.let*
.closesttozero
(define closesttozero (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 (closesttozero (cdr lst)))) (car lst)) ; Otherwise, use the thing in the remainder closest to zero (else (closesttozero (cdr lst))))))
closesttozeo
may get called repeatedly with the same parameters. (We'll check that with some sample code.)
closesttozero
, we
can make one by naming the result and using it twice. One
possibility is to use a helper procedure
(define closesttozero (lambda (lst) (if (null? (cdr lst) (car lst) (closertozero (car lst) (closesttozero (cdr lst))))))) (define closertozero (lambda (guess1 guess2) (if (< (abs guess1) (abs guess2)) guess1 guess2)))
(define closesttozero (lambda (lst) (cond ((null? (cdr lst)) (car lst)) "Compute (closesttozero (cdr lst)) and call it guess" (if (< (abs (car lst)) (abs guess)) (car lst) guess))))
let
let
.
let
has the form
(let ((name_{1} exp_{1}) (name_{2} exp_{2}) ... (name_{n} exp_{n})) body)
let
has the meaning:
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))
let
within a procedure.
Here's a new version of closesttozero
that uses
let
.
(define closesttozero (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 (closesttozero (cdr lst)))) ; Choose the closer to zero of the first element and guess (if (< (abs (car lst)) (abs guess)) (car lst) guess)))))
let*
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:
closesttozero
that makes closertozero
a helper.
(define closesttozero (let ((closertozero (lambda (guess1 guess2) (if (< (abs guess1) (abs guess2)) guess1 guess2)))) (lambda (lst) (if (null? (cdr lst)) (car lst) (closertozero (car lst) (closesttozero (cdr lst)))))))
;;; Procedure: ;;; exactaverage ;;; 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 exactaverage (lambda (num1 num2) (let ((verify? (lambda (val) (and (number? val) (exact? val))))) (cond ((not (verify? num1)) (error "exactaverage" "first parameter is a nonnumber")) ((not (verify? num2)) (error "exactaverage" "second parameter is a nonnumber")) (else (/ (+ num1 num2) 2))))))
Back to Discussion of Exam 1. On to Local Bindings Laboratory.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]

[Honesty]
[Instructions]
[Links]
[Guidelines for Lab Writeups]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[Scheme Report]
[Glimmer Scheme Reference]
[CSC151.01 (Gum)]
[CSC151 2003S]
[CSC151 2002F]
[SamR]
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 Tue Dec 9 13:59:44 2003.
The source to the document was last modified on Mon Sep 1 13:30:51 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Outlines/outline.24.html
.
You may wish to validate this document's HTML ; ; Check with Bobby
Samuel A. Rebelsky, rebelsky@grinnell.edu