CSC 153  Grinnell College  Spring, 2005 
Computer Science Fundamentals  
Laboratory Exercise  
This laboratory exercise explores three new, but related, mechanisms to associate values with variables:
What are the values of the following let
expressions?
(let ((tone "fa") (callme "al")) (stringappend callme tone "l" tone))
;; solving 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))))
(let ((sum (+ 8 3 4 2 7))) (let ((mean (/ sum 5))) (* mean mean)))
You may use Chez Scheme to help you answer these questions, but be sure you can explain how it arrived at its answers.
Consider the following countallsymbols
procedure which
counts the number of symbols that appear in a list or any of its sublists.
A sample run also is given:
(define countallsymbols (lambda (ls) ;Precondition: ls is a list ;Postcondition: returns the number of symbols on ls or any sublist of ls (cond ((null? ls) 0) ((symbol? (car ls)) (+ 1 (countallsymbols (cdr ls)))) ((list? (car ls)) (+ (countallsymbols (car ls)) (countallsymbols (cdr ls)))) (else (countallsymbols (cdr ls)))))) (countallsymbols '(((a b) c) d (e (f)))) ===> 6
Rewrite this countallsymbols procedure, using a letexpression to consolidate repeated subexpressions in the same manner.
Write a nested let
expression that binds a total of five
variables, a
, b
, c
, d
,
and e
, with a
bound to 9387 and each subsequent
variable 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 variables.
Write a let*
expression equivalent to the
let
expression in the previous exercise.
Rewrite the longestonlist
procedure from the first lab on recursion so that it incorporates
the longerstring
procedure by means of a local binding.
Write a letrec
expression in which (a) the identifier
lastoflist
is locally bound to a procedure that finds and
returns the last element of a given list, and (b) the body of the
expression computes the sum of the last elements of the lists (3 8
2)
, (7)
, and (8 5 9 8)
, invoking
lastoflist
three times.
The letrec
expression makes it possible to include binding
specifications for mutually recursive procedures in the same binding list:
(letrec ((upsum (lambda (ls) (if (null? ls) 0 (+ (downsum (cdr ls)) (car ls))))) (downsum (lambda (ls) (if (null? ls) 0 ( (upsum (cdr ls)) (car ls)))))) (upsum '(1 23 6 12 7)) ) ===> 21
A nonempty list is an snalternator if its elements are alternately symbols and numbers, beginning with a symbol. It is an nsalternator if its elements are alternately numbers and symbols, beginning with a number.
Using the letrec
definitions of upsum and
downsum as an example, write a letrec
expression in
which (a) the identifiers snalternator?
and
nsalternator?
are bound to mutually recursive predicates,
each of which determines whether a given nonempty list has the indicated
characteristic, and (b) the body invokes each of these predicates to
determine whether the list (2 a 3 b 4 c 5)
fits either
description.
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/153.sp05/labs/localbindings.shtml
created February 26, 1997 last revised February 1, 2005 

For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. 