Remember to follow the Expectations on Homework when preparing your solutions.
Start with these data definitions from Homework 5 and 6:
;; a borrower is a (make-borrower string number string string number number) (define-struct borrower (name team-size country business requested percent-raised)) ;; a list-of-borrower is either ;; empty or ;; (cons borrower list-of-borrower)Here is a set of solutions to Homework 6 that you may refer to as you work on Problems 1 and 2.
find-by-country
, with the
following contract and purpose:
;; find-by-country: string list-of-borrower -> list-of-borrower ;; consumes the name of a country and a list of borrowers and produces a ;; list of only those borrowers from the given countryWrite a new version of
find-by-country
that uses filter
.
foldl
. foldl
iterates a function over the items in a list to build up a return value. For example, you can use foldl
to
sum the numbers in a list of numbers like this:
(foldl + 0 (list 3 4 5))
foldl
consumes a function, an initial value, and a list.
In the given example, the function being applied to the items in the list is +,
the initial value is 0, and the list to be summed is (list 3 4 5 )
.
The return value in this case is 12.
Use foldl
and map
to rewrite another function from
Homework 6, funds-needed
. Here's the contract and purpose:
;; funds-needed: list-of-borrower -> number ;; consumes a list of borrowers and produces the sum of all amounts still needed(You may also want to make use of the helper from Homework 6,
funds-of-one
.)
;; A bintree[alpha] is either ;; - false, or ;; - a node ;; a node is a (make-node alpha bintree bintree) (define-struct node (item left right))
boolean?
sign-tree
that consumes a bintree[number]
(i.e., a bintree where the items at each node are numbers) and returns a bintree[symbol] with the same structure as the input tree, but with each number replaced with one of the symbols 'positive, 'negative, or 'zero, as appropriate.
posn
that's built-in to DrRacket. A
posn
can be used to represent the x and y coordinates of a point.
Here is the data definition for a posn:
;; a posn is a struct ;; (make-posn number number) (define-struct posn(x y))Write a program
flip-tree
that consumes a bintree[posn] and returns a bintree[posn]. The returned tree should have the same structure as the input tree, but each posn should have its x- and y-coordinates flipped [i.e., (make-posn 3 4) would become (make-posn 4 3)].
sign-tree
and flip-tree
have a
similar structure. Write a function tree-map
that takes a bintree and a function and applies the given function to each node in the tree. Show how to implement sign-tree
and flip-tree
using tree-map
. Be sure to include a correct contract for tree-map
.
tree-andmap
that consumes a function of type (alpha -> boolean) and a bintree[alpha] and returns a boolean indicating whether the given function returned true at every node.
Using tree-andmap
, implement pos-tree?
that consumes a bintree[number] and determines whether all numbers in the tree are positive.
Using web-based turnin, turn in a single file containing all code and documentation for this assignment. Follow the naming conventions when naming your file. Please make sure both partners' names and wpi login names are listed in a comment at the top of the file.