Fundamentals of CS I (CS151 2001S) : Outlines
[Current]
[Discussions]
[Glance]
[Honesty]
[Instructions]
[Links]
[News]
[Search]
[Syllabus]
Primary
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Quizzes]
[Readings]
[Reference]
Sets
[Blackboard]
[Scheme Report]
[SamR's Schedule]
[Rebelsky/Fall 2000]
[Walker/Fall2000]
[Stone/Spring2000]
Links
Reading: Naming Values with Local Bindings. Back to Preconditions and Postconditions. On to Laboratory: Local Bindings.
Held Wednesday, February 28, 2001
Summary
Today we consider how to bind names to values using Scheme's let
expressions.
Notes
Overview
let
let*
assoc which
returns a list of all of the matching values, if there are any;
and #f otherwise.
(define assoc-all
(lambda (key database)
; Nothing can be in the empty database, so return false.
(if (null? database)
#f
; If we match the car of the database, look in the rest.
(if (equal? key (car (car database)))
; If the key does not appear any more in the database
; make a list of just the one value we've seen
(if (not (assoc-all key (cdr database)))
(list (car database))
; Otherwise, attach the matched entry to
; the remaining matched entries
(cons (car database) (assoc-all key (cdr database))))
; We didn't match the car, so just look in the rest
(assoc-all key (cdr database)))))))
(car database) at
least twice in each recursive call and assoc-all
twice in some.
assoc-all twice? Once to determine if
there are any remaining elements and once to use those remaining
elements.
letlet.
let has the form
(let ((name1 exp1)
(name2 exp2)
...
(namen expn))
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 assoc-all that uses
let.
(define assoc-all
(lambda (key database)
; Nothing can be in the empty database, so return false.
(if (null? database)
#f
; Otherwise, we need to look more closely.
(let ((element (car database)) ; Get one element
(other-matches ; Find any other matching elements
(assoc-all key (cdr database))) )
; If the current element matches ...
(if (equal? key (car (element)))
; If it also appears elsewhere, join 'em together.
(if (other-matches)
(cons element other-matches)
; Otherwise, just make a list of the one value
(list (car database)))
; If the current element does not match, just look
; in the rest.
(assoc-all key (cdr database)))))))
let*let* rather than let.
let* has the form
(let* ((name1 exp1)
(name2 exp2)
...
(namen expn))
body)
let* has the meaning:
;;; 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))))))
Back to Preconditions and Postconditions. On to Laboratory: Local Bindings.
[Current]
[Discussions]
[Glance]
[Honesty]
[Instructions]
[Links]
[News]
[Search]
[Syllabus]
Primary
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Quizzes]
[Readings]
[Reference]
Sets
[Blackboard]
[Scheme Report]
[SamR's Schedule]
[Rebelsky/Fall 2000]
[Walker/Fall2000]
[Stone/Spring2000]
Links
Disclaimer: I usually create these pages on the fly. This means that they are rarely proofread and may contain bad grammar and incorrect details. It also means that I may update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.
This page was generated by Siteweaver on Wed May 5 12:14:57 2004.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/outline.22.html.
You may validate
this page's HTML.
The source was last modified Tue Jan 23 16:01:57 2001.