Practice Scheme Assignment
CS 550 Programming Languages
Instructor: Jeremy Johnson
Functional Programming in Scheme
This assignment provides an introduction to functional programming and the
lambda calculus. You will write several simple recursive functions in the
scheme programming language.
Background information is obtained in
lecture on functional programming and
the documentation for
What to do
Implement, document (i.e. specifications), and test the following functions in scheme. Scheme has implemented some of the constructs below (iota, association lists) - you may not use these implementations, but rather must implement them
yourself. Make sure all functions are thoroughly tested.
- The Matlab language supports a convenient notation for specifying
ranges of numbers. The notation start:step:end denotes the
range of integers start, start+step, start+2*step,...,start+n*step,
where n is the largest integer such that start+n*step &le end and
start+(n+1)*step > end.
Note that the range may be empty if start > end.
Write a scheme function (range (start step end)), which returns
the list of integers equal to
Example: (range '(0 2 7)) => (0 2 4 6), (range '(2 2 0)) => ()
- The Maple computer algeba system has a command
seq(f, i = m..n, step), which returns the sequence
fm,...fn, where fi is the expression f with all occurrences of the
symbol i replaced by the numeric value of i in the sequence of
integers from m to n. Implement a scheme function
(seq f (start step end)), and produces a list of values
(f(start),f(start+step),...,f(start+n*step)), where n is the largest
integer such that start+n*step &le end and start+(n+1)*step > end.
Example: (seq (lambda (x) (* x x)) '(0 2 7)) =>
(0 4 16 36)
- Write a scheme function (iterator (start step end)) which
returns a function which when repeatedly called returns the numbers
in the sequence (range (start step end)). When the sequence
is exhausted the returned function should return ().
Example: (define next (iterator '(0 2 7))),
(begin (next) (next) (next) (next) (next)) => 0, 2, 4, 6, ()
- An association list is a list of bindings of names to values:
((name1 val1) ... (namet valuet)). This data structure can be used
to implement a symbol table. An environment can be represented by
a list of association lists (i.e. a list of symbol tables), where
the first element in the list is nearest scope, the second the next
surrounding scope, and the last the outermost scope.
- Write recursive scheme function (lookup name assoc_list)
that returns the binding (pair) whose name equals the given
name. If no such binding is found return the null list.
- Write a recursive function (lookup-env name environment),
which returns the binding with the specified name in an
environment (i.e. list of association lists) and null if no such
binding is found.