Module 2

Module 2 - Module 2 More functional abstraction What can we...

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

View Full Document Right Arrow Icon
Module 2 More functional abstraction What can we do with functions as values? CS 116: Introduction to Computer Science 2 Daniel G. Brown/Troy Vasiga, University of Waterloo 2.1 Purpose of Module 2 Making abstract functions: functions that use functions as arguments Making single-use functions: functions that don’t need a name Making functions that return functions: functions that are needed for further use Readings: HtDP 21, 22.1, 22.2, Intermezzo 4 2.2 1 More functional abstraction Functional abstraction The three most common uses of functions on functions are built-in Scheme functions: map filter foldr These are not the only examples of when we’ll need to apply specified functions to inputs. How do we write functions that use functions? Let’s start with how to implement map . 2.3 Implementing map map reads in a function func and a list lst . It applies func to every element of the lst . It collects the results in a new list. So we’ll use the list template, it seems: ( define ( map func lst ) ( cond [( empty ? lst ) ...] [ else ... ( first lst ) ... ... ( map func ( rest lst )) . ..])) The base case is clearly empty . 2.4 The recursive case for map Consider the recursive case. If we have executed func on all of the rest of the list: We need to join the value of ( func ( first lst )) to the value we got from the rest of the list. So we want to use cons to put things together. 2.5 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The code for map The correct code is: ( define ( map func lst ) ( cond [( empty ? lst ) empty ] [ else ( cons ( func ( first lst )) ( map func ( rest lst )))])) This works, but what’s this function’s contract? We need to be able to have a type for the function func . We can get the answer from its contract. 2.6 map and its contract Our functions have contracts, which tell us what type of functions they are. For example, the type of sqr is num num . We have other rules for lists: the type of ( list 1 2 3) is ( listof int ) . So, for example, a function sum-list that adds up the elements of a list of numbers would have the contract ( listof num ) num . This gives a process to build contracts of functions, and is helpful when we want to know if our functions are going to mesh with each other properly. What about for map ? Again, what’s its contract? 2.7 map and its contract One possibility: map : ( num num ) ( listof num ) ( listof num ) This says that map reads in a function from numbers to numbers, and a list of numbers, and makes a list of numbers from them. But that’s not all map can do. We could use map to change a list of posn s into a list of numbers: ( map posn-x posnlist ) maps every posn in posnlist onto the x-axis. So is it:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/21/2010 for the course CS 116 taught by Professor T.vasiga during the Fall '09 term at Waterloo.

Page1 / 8

Module 2 - Module 2 More functional abstraction What can we...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online