Fundamentals of CS I (CS151 2002F)

**Primary:**
[Skip To Body]
[Front Door]
[Current]
[Glance]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]

**Groupings:**
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]

**ECA:**
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]

**Miscellaenous:**
[Scheme Reference]
[CS151 2002F Gum]
[CS151 2001S]
[SamR]
[Glimmer Labs]
[schemers.org]

a. Review the corresponding notes on
`letrec`

and named let.

b. Start DrScheme.

a. Define a recursive procedure, `last-of-list`

, a
*recursive* procedure that returns the last element of
a list.

b. Using that procedure, compute the sum of the last elements of
the lists
`(3 8 2)`

, `(7)`

, and
`(8 5 9 8)`

.

Note that you will probably need to make three calls to
`last-of-list`

.

c. Rewrite your solutions to the previous two problems using
a `letrec`

-expression in which

- the identifier
`last-of-list`

is locally bound to a*recursive*procedure that finds and returns the last element of a given list, and - the body of the
expression computes the sum of the last elements of the lists
`(3 8 2)`

,`(7)`

, and`(8 5 9 8)`

.

The body of your expression should invoke `last-of-list`

three times.

Note that you are to write an expression and not a procedure (other than the
local `last-of-list`

) for part c of this exercise.

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

- 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 - the body invokes
each of these predicates to determine whether the list
`(2 a 3 b 4 c 5)`

fits either description.

Your `letrec`

expression should have the form

(letrec ((s-n-alternator? ...) (n-s-alternator? ...)) ...)

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 an appropriate kernel inside a husk that
performs precondition testing. This version of `iota`

should
look something like

(define iota (lambda (num) (letrec ((kernel ...)) ...)))

b. Define and test a version of the `iota`

procedure that uses
a named `let`

. This version of `iota`

should
look something like

(define iota (lambda (num) ... (let kernel (...) ...)))

Define and test a procedure named `take`

that takes a list
`lst`

and a non-negative integer `len`

as arguments
and returns a list consisting of the first `len`

elements of
`lst`

, in their original order.

The procedure should signal an error if `lst`

is not a list, if
`len`

is not an exact integer, if `len`

is negative,
or if `len`

is greater than the length of `lst`

.

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 [Samuel A. Rebelsky]

- Created.
- Most of the problems were taken from
`http://www.cs.grinnell.edu/~stone/courses/scheme/local-binding-and-recursion.xhtml`

- This version is available at
`http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000F/Labs/letrec.html`

Wednesday, 14 March 2001 [Samuel A. Rebelsky]

- Updated formatting.
- Slight modifications to some problems.
- This version is available at
`http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/Labs/letrec.html`

Tuesday, 15 October 2002 [Samuel A. Rebelsky]

- Updated formatting.
- Updated wording on a few problems.
- Added illustrations of the expected form of answers for the first few problems. (After class.)
- Added steps to the first problem.
- This version is available at
`http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Labs/letrec.html`

**Primary:**
[Skip To Body]
[Front Door]
[Current]
[Glance]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]

**Groupings:**
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]

**ECA:**
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]

**Miscellaenous:**
[Scheme Reference]
[CS151 2002F Gum]
[CS151 2001S]
[SamR]
[Glimmer Labs]
[schemers.org]

**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 Mon Dec 2 09:18:58 2002.

The source to the document was last modified on Tue Oct 15 11:20:19 2002.

This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Labs/letrec.html`

.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu