CS51 Assignment 3:
λ
!
Due:
Friday, February 27th, 2009, 5:00PM
Total Points: 56 (including 10 style points)
In this assignment, you will discover the magic of higherorder functions.
You will
implement analogues of
fold
and
map
for binary trees, and put them to various uses,
including a solver for a simple game.
For this assignment, you should strive to maximize
elegance rather than speed.
1
HigherOrder Functions
(9 points total)
One of the most powerful aspects of Scheme – as well as other functional programming
languages – is that its functions are firstclass values. This means that you can treat functions
the same way you treat any other datatype in Scheme.
Beyond simply calling functions,
this means that functions can take functions as arguments and can even return functions.
Functions that do either of these things are called
higherorder functions
.
1.1
Lambda
Scheme provides a special form,
lambda
, that allows us to create anonymous functions, or
functions that don’t have names. Up until now, we’ve written our functions
(
(
foo
bar
)
bar
)
which creates a named function
foo
. However, this is really syntactic sugar
1
for
(
foo
(
(
bar
)
bar
))
In both expressions, we’ve created a function and bound it to
foo
. In the first expression,
however, we created and named the function all in one step. In the second expression, the
process of giving the function a name is separate from the process of creating the function.
In fact, it’s best to think of functions as entities that
may
be bound to a symbol. When we
1
That is, a convenient, but equivalent, alternative syntax.
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
1
HIGHERORDER FUNCTIONS
CS51 Assignment 3:
λ
!
write
(define foo 5)
we are binding the symbol
foo
to the number 5; we are
not
giving
the number 5 the name
foo
. Try to think about functions the same way.
So, what’s the syntax of
lambda
? It’s just
(lambda (<arguments>) <expression>)
. A
function that returns its only argument is just:
(
(
x
)
x
)
lambda
becomes really powerful when we return functions from other functions. Within
the body of the
lambda
, we can refer to variables that are in the scope of the function. A
trivial example is creating a function that returns the same value for all inputs.
We can
write
createconstantfunction
, which returns such a function, like this:
(
(
createconstantfunction
n
)
(
(
x
)
n
))
((
createconstantfunction
5)
false
) =
>
5
((
createconstantfunction
100)
1) =
>
100
((
createconstantfunction
false
)
empty
) =
>
false
This probably seems a little weird, but you’ll get the hang of it.
Exercise 1.
9 points
Now you get to write your own higherorder functions!
(a)
[2 points] Write
nadder
, which takes an argument
n
and returns a function that takes
a single argument
x
and returns the sum of
x
and
n
.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '09
 GREGMORRISETT
 Functional Programming

Click to edit the document details