{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# ps03 - CS51 Assignment 3 Due Friday February 27th 2009...

This preview shows pages 1–3. Sign up to view the full content.

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 higher-order 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 Higher-Order Functions (9 points total) One of the most powerful aspects of Scheme – as well as other functional programming languages – is that its functions are first-class 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 higher-order 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 HIGHER-ORDER 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 create-constant-function , which returns such a function, like this: ( ( create-constant-function n ) ( ( x ) n )) (( create-constant-function 5) false ) = > 5 (( create-constant-function 100) 1) = > 100 (( create-constant-function 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 higher-order functions! (a) [2 points] Write n-adder , 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.

{[ snackBarMessage ]}

### Page1 / 5

ps03 - CS51 Assignment 3 Due Friday February 27th 2009...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online