Problem Set 6
Language: Intermediate Student with lambda
Due: 11:59 PM Thursday, Feb. 17
1 Practice Abstracting
Click the EECS 111 Handin button.
Select Problem Set 6 in the Assignment drop-down menu.
Check the Retrieve checkbox.
The retrieved file contains the data definitions for person and database, as well as definitions and tests of the create-database and create-database-from-list functions. Add your code to this file and submit the whole thing when you’re done.
Note there is a slight change from Problem Set 4 in the definition of create-database: the relative order of the function’s parameters is flipped, making the person argument the first argument instead of the second. With this change, the definition of create-database-from-list closely resembles the following definition of sum.
; sum: list-of-number -> number |
; Produces the sum of the given numbers |
(define (sum lon) |
(cond [(empty? lon) 0] |
[(cons? lon) (+ (first lon) (sum (rest lon)))])) |
(check-expect (sum empty) 0) |
(check-expect (sum (list 2 5 -1 3)) 9) |
Abstract the common parts of these definitions of create-database-from-list and sum into a new function called reduce. Redefine create-database-from-list and sum in terms of reduce.
2 Practice with map and filter
Use map and filter, along with lambda, to define the following functions.
; pts-north : posn list-of-posn -> list-of-posn |
; returns the posns from lop that are north of p, |
; that is, whose y-coordinate is greater than p's y-coordinate |
(define (pts-north p lop) ---) |
; build-straight-line : num list-of-number -> list-of-posn |
; returns a list of posns where the x-coordinate is n and |
; the y-coordinate is a number in lon |
; e.g., (build-straight-line 2 (list 1 2 3)) |
; should be (list (make-posn 2 1) (make-posn 2 2) (make-posn 2 3)) |
(define (build-straight-line n lon) ---) |