This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 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 dont have names. Up until now, weve written our functions ( define ( foo bar ) bar ) which creates a named function foo . However, this is really syntactic sugar 1 for ( define foo ( lambda ( bar ) bar )) In both expressions, weve 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, its 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 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, whats the syntax of lambda ? Its just (lambda (<arguments>) <expression>) . A function that returns its only argument is just: ( lambda ( 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: ( define ( create-constant-function n ) ( lambda ( 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 youll get the hang of it. Exercise 1. 9 points Now you get to write your own higher-order functions!...
View Full Document
This note was uploaded on 07/26/2009 for the course COMPUTERSC CS51 taught by Professor Gregmorrisett during the Spring '09 term at Harvard.
- Spring '09