Computer Science Fundamentals (CS153 2003S)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]

[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
Start DrScheme.
let
What are the values of the following let
expressions?
You may use DrScheme to help you answer these questions, but be sure you
can explain how it arrived at its answers.
a.
(let ((tone "fa") (callme "al")) (list callme tone "l" tone))
b.
;; Solutions to the quadratic equation x^2  5x + 4: (let ((discriminant ( (* 5 5) (* 4 1 4)))) (list (/ (+ ( 5) (sqrt discriminant)) (* 2 1)) (/ ( ( 5) (sqrt discriminant)) (* 2 1))))
c.
(let ((total (+ 8 3 4 2 7))) (let ((mean (/ total 5))) (* mean mean)))
Write a nested let
expression that binds a total of five
names, alpha
, beta
, gamma
,
delta
, and epsilon
, with alpha
bound to 9387 and each subsequent name bound to a value twice
as large as the one before it  beta
should be
twice as large as alpha
, gamma
twice as
large as beta
, and so on. The body of the innermost
let
expression should then compute the sum of the values
of the five names.
Write a let*
expression equivalent to the
let
expression in the previous exercise.
Consider the following procedure that squares all the values in a list.
;;; Procedure: ;;; squarevalues ;;; Parameters: ;;; lst, a list of numbers of the form (num_1 num_2 ... num_n) ;;; Purpose: ;;; Squares all the values in lst. ;;; Produces: ;;; listofsquares, a list of numbers ;;; Preconditions: ;;; [Standard] ;;; Postconditions: ;;; listofsquares has the form (square_1 square_2 ... square_n) ;;; For all i, square_i is the square of num_i (that is num_i * num_i). (define squarevalues (lambda (lst) (let ((square (lambda (val) (* val val)))) (if (null? lst) null (cons (square (car lst)) (squarevalues (cdr lst)))))))
a. Verify that squarevalues
works correctly.
b. Try to execute square
outside of squarevalues
.
Explain what happens.
Here is a procedure that takes a nonempty list of lists as an argument and returns the longest list in the list (or one of the longest lists, if there is a tie).
;;; Procedure: ;;; longestlistinlist ;;; Parameters: ;;; los, a list of lists ;;; Purpose: ;;; Finds one of the longest lists in los. ;;; Produces: ;;; longest, a list ;;; Preconditions: ;;; los is a nonempty list. ;;; every element of los is a list. ;;; Postconditions: ;;; Does not affect los. ;;; Returns an element of los. ;;; No element of los is longer than longest. (define longestlistinlist (lambda (los) ; If there is only one list, that list must be the longest. (if (null? (cdr los)) (car los) ; Otherwise, take the longer of the first list and the ; longest remaining list. (longerlist (car los) (longestlistinlist (cdr los))))))
This definition of the longestlistinlist
procedure
includes a call to the longerlist
procedure, which returns
the longer of two given lists:
;;; Procedure: ;;; longerlist ;;; Parameters: ;;; left, a list ;;; right, a list ;;; Purpose: ;;; Find the longer of left and right. ;;; Produces: ;;; longer, a list ;;; Preconditions: ;;; Both left and right are lists. ;;; Postconditions: ;;; longer is a list. ;;; longer is either equal to left or to right. ;;; longer is at least as long as left. ;;; longer is at least as long as right. (define longerlist (lambda (left right) (if (<= (length right) (length left)) left right)))
Revise the definition of longestlistinlist
so that the
name longerlist
is bound to the procedure that it denotes
only locally, in a let
expression.
Note that there are at least two possible ways to do the previous exercise:
The definiens of
longestlistinlist
can be a lambda
expression
with a let
expression as its body, or it can be a
let
expression with a lambda
expression as its
body. That is, it can take the form
(define longestlistinlist (let (...) (lambda (los) ...)))
or the form
(define longestlistinlist (lambda (los) (let (...) ...)))
a. Define longestlistinlist
in whichever way that you did not
define it for the previous exercise.
b. Does the order of nesting affect what happens when the procedure is invoked? If so, which arrangement is better? Why?
c. Make a similar change to squarevalues
.
The two definitions you came up with in the previous exercises
are not the only alternatives you have in placing the let
.
Since longerlist
is only needed in the recursive case,
you can place the let
there.
(define longestlistinlist (lambda (los) ; If there is only one list, that list must be the longest. (if (null? (cdr los)) (car los) ; Otherwise, take the longer of the first list and the ; longest remaining list. (let ((longerlist (lambda (left right) (if (<= (length right) (length left)) left right)))) (longerlist (car los) (longestlist inlist (cdr los))))))
Including the original definition, you've now seen or written four
variants of longestlistinlist
. Which do you prefer?
Why?
Extend your favorite version of longestlistinlist
so
that it verifies its preconditions (i.e., that los
only
contains lists and that los
is nonempty). If the
preconditions are not met, your procedure should return #f
.
It is perfectly acceptable for you to check each list element in turn to determine whether or not it is a list, rather than to check them all at once, in advance.
Using DrScheme's (time exp)
operation, determine
which of the four versions of longestlistinlist
is
indeed the fastest. You should try a variety of lengths for the
outer list. The inner lists can be mostly 0 or 1element lists..
Monday, 2 October 2000 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~stone/courses/scheme/spring2000/localbindings.xhtml
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000S/Labs/localbindings.html
Wednesday, 28 February 2001 [Samuel A. Rebelsky]
Thursday, 1 March 2001 [Samuel A. Rebelsky]
alpha
to epsilon
rather than a
to e
because the a
was too frequently read as an article rather than a name.
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/Labs/let.html
.
Monday, 7 October 2002 [Samuel A. Rebelsky]
Tuesday, 8 October 2002 [Samuel A. Rebelsky]
Wednesday, 9 October 2002 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Labs/let.html
.
Sunday, 2 February 2003 [Samuel A. Rebelsky]
longeststringonlist
problems with
longestlistinlist
.
http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Labs/let.html
.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]

[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[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 May 6 09:19:52 2003.
The source to the document was last modified on Sun Feb 2 22:45:31 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Labs/let.html
.
You may wish to validate this document's HTML ; ; Check with Bobby
Samuel A. Rebelsky, rebelsky@grinnell.edu