Fundamentals of Computer Science I (CS151.02 2007S)
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]
-
[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 2006F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
This outline is also available in PDF.
Held: Friday, February 16, 2007
Summary:
Today we consider how to bind names to values using Scheme's various kinds of let
expressions.
Related Pages:
Due
Assignments
Notes:
Overview:
let
.let*
.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))))))
closest-to-zeo
may get called repeatedly with the same parameters. (We'll check that with some sample code.)
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) (if (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)))
(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))))
let
let
.
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 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*
let*
rather than let
.
let*
has the form
(let* ((name1 exp1) (name2 exp2) ... (namen expn)) body)
let*
has the meaning:
closest-to-zero
that makes closer-to-zero
a helper.
(define closest-to-zero (let ((closer-to-zero (lambda (guess1 guess2) (if (< (abs guess1) (abs guess2)) guess1 guess2)))) (lambda (lst) (if (null? (cdr lst)) (car lst) (closer-to-zero (car lst) (closest-to-zero (cdr lst)))))))
;;; 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))))))
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]
-
[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 2006F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
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 Thu Sep 13 20:54:43 2007.
The source to the document was last modified on Thu Jan 18 12:30:12 2007.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2007S/Outlines/outline.16.html
.
You may wish to
validate this document's HTML
;
;
http://creativecommons.org/licenses/by-nc/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.