1 / 9

CS 116 Tutorial 2

CS 116 Tutorial 2 . Functional Abstraction. Reminders:. Assignment 2 is due on Tuesday , January 28th at 11:00am.

ferris
Download Presentation

CS 116 Tutorial 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 116 Tutorial 2 Functional Abstraction

  2. Reminders: • Assignment 2 is due on Tuesday, January28th at 11:00am

  3. 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)

  4. 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)

  5. 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))

  6. 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.

  7. 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.

  8. 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?

  9. 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?

More Related