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]
A few of you have asked me to provide some more information on
let and let*, including more examples.
I hope that this document will help alleviate some of the
confusion.
The let and let* procedures allow
you to name values. There are three basic reasons
to name values:
read and random) that may return
a different value each time and we need to use the result
of one call multiple times, we need to name that result.
Here are some examples of each.
Suppose we've decided to represent some compound value as a list. For example, we might want to represent someone's name as a five-value list (title, first-name, middle-name, last-name, suffix). Now, suppose we want to print the last name of all the people whose title is "Professor".
(define search-for-profs
(lambda (people)
(if (not (null? people))
(begin
(if (string=? (car (car people)) "Professor")
(begin
(display (list-ref (car people) 3))
(newline)))
(search-for-profs (cdr people))))))
Here's a sample call:
(search-for-profs
(list (list "Professor" "Samuel" "Alexander" "Rebelsky" "")
(list "Doctor" "Michelle" "Steele" "Rebelsky" "")
(list "Professor" "Freda" "Gould" "Rebelsky" "")
(list "Mister" "William" "" "Rebelsky" "")
(list "Mister" "William" "Lloyd" "Rebelsky" "")
(list "Mister" "Jonathan" "Bennett" "Rebelsky" "")))
One problem with this procedure is that may not be clear to the reader why we're taking the car of the car of people or element 3. Here is some clearer code.
(define search-for-profs
(lambda (people)
(if (not (null? people))
(begin
(let* ((one-person (car people))
(remaining-people (cdr people))
(title (car one-person))
(lastname (list-ref one-person 3)))
(if (string=? title "Professor")
(begin
(display lastname))
(newline)))
(search-for-profs remaining-people)))))
Consider the problem of reading a value and printing out the type of value read. Here's one (incorrect) solution.
(display "Enter a value and I'll tell you its type: ")
(cond
((symbol? (read))
(begin
(display (read))
(display " is a symbol")
(newline)))
((string? (read))
(begin
(display (read))
(display " is a string")
(newline)))
(else
(begin
(display "I can't figure out the type of ")
(display (read))
(newline))))
What's wrong with this? Well, every time that the code calls
(read), the code attempts to read a new
value. Hence, even if it reads a symbol for the first value,
it still needs to read something else before printing the result.
(Try it and see).
So, what should we do? We should read the value once and name it.
(display "Enter a value and I'll tell you its type: ")
(let ((val (read)))
(cond
((symbol? val)
(begin
(display val)
(display " is a symbol")
(newline)))
((string? val)
(begin
(display val)
(display " is a string")
(newline)))
(else
(begin
(display "I can't figure out the type of ")
(display val)
(newline)))))
Monday, 12 March 2001
Monday, 9 April 2001
Readings/more-let.html.
[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 14:00:19 2003.
The source to the document was last modified on Mon Sep 1 13:26:32 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Readings/more-let.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby