[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]
Read the corresponding notes on
letrec
and name let.
Write a letrec
-expression in which (a) the identifier
last-of-list
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
last-of-list
three times.
A non-empty list is an s-n-alternator if its elements are alternately symbols and numbers, beginning with a symbol. It is an n-s-alternator if its elements are alternately numbers and symbols, beginning with a number.
Write a letrec
-expression in which (a) the identifiers
s-n-alternator?
and n-s-alternator?
are bound to
mutually recursive predicates, each of which determines whether a given
non-empty 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.
As you may recall, Iota
takes a natural number as argument
and returns a list of all the lesser natural numbers in ascending order.
a. Define and test a version of the iota
procedure that uses
letrec
to pack a tail-recursive kernel inside a husk that
performs precondition testing.
b. Define and test a version of the iota
procedure that uses
a named let
.
Define and test a procedure named take
that takes a list
ls
and a non-negative integer len
as arguments
and returns a list consisting of the first len
elements of
ls
, in their original order. The procedure should signal an
error if ls
is not a list, if len
is not an exact
integer, if len
is negative, or if len
is greater
than the length of ls
.
a. Define and test a procedure named intersection
that takes two
lists of symbols, left
and right
, as arguments
and returns a list of which the elements are precisely those symbols that
are elements of both left
and right
.
b. What does your proceduer do if a symbol appears in both lists and appears more than once in one or both of the lists?
Monday, 24 October 2000
http://www.cs.grinnell.edu/~stone/courses/scheme/local-binding-and-recursion.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/letrec.html
Source text last modified Mon Oct 23 10:42:12 2000.
This page generated on Mon Oct 23 10:44:40 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu