90 likes | 191 Views
CS 116 Tutorial 2 . Functional Abstraction. Reminders:. Assignment 2 is due on Tuesday , January 28th at 11:00am.
E N D
CS 116 Tutorial 2 Functional Abstraction
Reminders: • Assignment 2 is due on Tuesday, January28th at 11:00am
Using build-list, write a Scheme function powers-of-twothat consumes a nonnegative integer n and produces a list of integers that are the powers of 2 from 2n down to 20 = 1. • For example, (powers-of-two 2) => (list 4 2 1)
Using build-list, 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)
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))
Write a function map-posns that consumes a function (f) and a list of posn structures (points) and produces a new list by applying fto 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) • When applying f to each posn in points, the x-coordinate will always be the first parameter and the y-coordinate will be the second.
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.
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?
ormapis another built-in abstract function in Scheme. It consumes a function f and a listlst, and produces true if applying f to any value in lstproduces 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?