Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 116 Tutorial 2 Functional Abstraction. Reminders Assignment 2 is due this Wednesday at Noon.

Similar presentations


Presentation on theme: "CS 116 Tutorial 2 Functional Abstraction. Reminders Assignment 2 is due this Wednesday at Noon."— Presentation transcript:

1 CS 116 Tutorial 2 Functional Abstraction

2 Reminders Assignment 2 is due this Wednesday at Noon

3 Review Build-list Lambda Function that produces a function

4 Build-list (build-list n f) ;;build-list: Nat (Nat->X) -> (listof X) Builds a list of size n from 0 to n-1, then applies f to every element in the newly created list. Produces: ( list (f 0 ) (f 1 ) … (f ( n-1 ) ) ) Size of list Applied to every element in the list; think of it like map

5 Lambda Single-use, nameless helper function (define (f p1 … pn) function body here) (lambda (p1 … pn) function body here) lambda

6 Function that produces a function ( define (f x y …) ( lambda (a b …) function body here ) ) Contract: f: _ _ … -> ( _ _ … ->_ ) Only job of f is to call the lambda function Lambda function takes care of all the work MUST use in order to produce a function Consumed values of f Produced value of f / contract for lambda

7 1. Using build-list, write a Scheme function powers-of-two that consumes a nonnegative integer n and produces a list of integers that are the powers of 2 from 2 n down to 2 0 = 1. ◦ For example, (powers-of-two 2) => (list 4 2 1)

8 2. Using build-lis t and filter, write a Scheme function factors that consumes a number n (at least 1) and returns a list of all positive factors of n in increasing order. ◦ For example, (factors 4) => (list 1 2 4)

9 3. Without using explicit recursion, write a function copies that consumes a natural number n and returns a list containing 1 copy of 1, followed by 2 copies of 2, etc., up to n copies of n. You may want to use the function flatten below. ;; flatten: (listof (listof X)) -> (listof X) ;; consumes a list of lists of type X and appends these ;; lists together to create a list of type X (define (flatten le) (foldr append empty le))

10 4. Write a function map-posns that consumes a function ( f ) and a list of posn structures (points) and produces a new list by applying f to the x- and y- fields of each posn in points. ◦ For example, (map-posns (list (make-posn 0 1 ) (make-posn 10 4) (make-posn -4 -4)))  (list -1 6 0)

11 5. Write a function char-count-function- maker that consumes a predicate and produces a one-argument function. The function that is produced consumes a string and produces the number of characters for which the predicate evaluates to true. ◦ For example, (char-count-function-maker char-upper- case?) produces a function that consumes a string s and produces the number of characters in s that are uppercase.

12 6. Write a function map-together that consumes a function f and two lists lst1 and lst2 of the same length, and returns the list: (list (f (first lst1)(first lst2)) (f (second lst1)(second lst2))...) that we get if we apply f to the first elements of both lists, then the second elements of both lists, and so on. What is the contract of map-together ?

13 7. ormap is another built-in abstract function in Scheme. It consumes a function f and a list lst, and produces true if applying f to any value in lst produces true. Otherwise, false is produced. ◦ For example, to determine if there are any even numbers in (list 3 7 6 1 0 9 7 -1), you can write (ormap even? (list 3 7 6 1 0 9 7 -1)). What is the contract for ormap?


Download ppt "CS 116 Tutorial 2 Functional Abstraction. Reminders Assignment 2 is due this Wednesday at Noon."

Similar presentations


Ads by Google