[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]
If you haven't done so already (or if you had problem understanding the short lecture), you may want to scan through the reading on local bindings.
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")
(call-me "al"))
(list call-me 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)))
d.
(let ((val (read))) (display "The square of ") (display val) (display " is ") (display (* val val)) (newline))
Suppose we are recording student information for Vivek's Dating Service in the following form:
(first-name last-name gender list-of-likes)
Write a procedure (likes thing list-of-students)
that extract all the students that like a particular thing. Use
let to simplify the process.
Write a nested let-expression that binds a total of five
names, a, b, c, d,
and e, with a bound to 9387 and each subsequent
name bound to a value twice as large as the one before it --
b should be twice as large as a, c
twice as large as b, and so on. The body of the innermost
let-expression should compute the sum of the values of the
five names.
Write a let*-expression equivalent to the
let-expression in the previous exercise.
Here is a procedure that takes a non-empty list of lists as argument and returns the longest list on the list (or one of the longest lists, if there is a tie).
(define longest-list-in-list
(lambda (ls)
(if (null? (cdr ls))
(car ls)
(longer-list (car ls) (longest-list-in-list (cdr ls))))))
This definition of the longest-string-in-list procedure
includes a call to the longer-string procedure, which returns
the longer of two given strings:
(define longer-string
(lambda (left right)
(if (<= (string-length right) (string-length left))
left
right)))
Revise the definition of longest-list-in-list so that the
name longer-list is bound to the procedure that it denotes
only locally, in a let-expression.
Note that there are two possible ways to do the previous exercise:
The definiens of
longest-list-in-list 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.
a. Define longest-list-in-list 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?
Monday, 2 October 2000
http://www.cs.grinnell.edu/~stone/courses/scheme/local-bindings.xhtml
[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000F/Labs/local-bindings.html
Source text last modified Mon Oct 2 10:46:10 2000.
This page generated on Mon Oct 2 10:49:11 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu