Held Wednesday, February 28, 2001
Today we consider how to bind names to values using Scheme's
assocwhich returns a list of all of the matching values, if there are any; and
(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-alltwice in some.
assoc-alltwice? Once to determine if there are any remaining elements and once to use those remaining elements.
lethas the form
(let ((name1 exp1) (name2 exp2) ... (namen expn)) body)
lethas the meaning:
letin a simple expression:
(define values (list 1 4 2 4 1 5 9)) (let ((largest (max values)) (smallest (min values))) (/ (+ largest smallest) 2))
letwithin a procedure. Here's a new version of
(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*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))))))
Friday, 12 January 2001
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
You may validate this page's HTML.
The source was last modified Tue Jan 23 16:01:57 2001.