Homework 9
Please read chapter 17 Nameless Functions, chapter 18 Summary, the sections labelled Intermezzo: Scope and Abstraction, Scope, and Pattern Matching and chapter 19 The Poetry of S-expressions in the textbook. You can ignore the section labelled ISL for Loops - we won't be learning that in this class. Also read all of chapter 6 and 8 in the Typed Racket Notes.
Be sure to include type, purpose, definition and tests for all functions.
Include the following at the top of your homework:
#lang typed/racket (require "../include/uchicago151.rkt") (require typed/test-engine/racket-tests)
You will also need the following batch of data structures:
And include (test) at the bottom of your homework.(define-struct Point ([x : Real] [y : Real])) (define-struct Line ([m : Real] [b : Real])) (define-struct Interval ([min : Real] [max : Real]))
Problem 1
Write the following functions using the built-in functions map, foldr, foldl, and filter, providing appropriate function arguments to these higher-order functions. You may use one, or more, of the listed functions to construct your solution; but, for credit, you must use at least one of these functions, rather than writing a recursive solution that directly processes a list.
Some of the problems refer to custom data types; please see above.
- (: first-quadrant ((Listof Point) -> (Listof Point))) which returns a list of only the points in the provided list that are in the first quadrant (do not include points on any axis)
- (: eval-f-at ((Real -> Real) (Listof Real) -> (Listof Point))) which evaluates the provided function at the specified x-values, returning a list of points containing the x-values and corresponding y-values.
- (: in-range (Interval (Listof Real) -> (Listof Real))) which takes a lower and upper bound, which are inclusive bounds, and returns a list of the values in the provided list that are in this range.
- (: project-y ((Listof Point) -> (Listof Real))) which projects each of the provided points onto the y-axis, in other words, gives their y-values
- (: on-line (Line (Listof Point) -> (Listof Point))) which, given a line (defined by its slope and y-intercept) and a list of points, returns those points that are actually on the line.
- (: max-num (Listof Real) -> Real) which given a list of reals, returns the largest one in the list. If the list is empty, it should return -inf.0.
- (: condfoldr : (All (A B) (-> (-> A Boolean) (-> A B B) B (Listof A) B))) which performs a foldr over the given list using the second function as the folding function, but skips over any values in the list for which the first function returns false (i.e., they do not contribute to the result of the fold and are not passed in to the second function)
- (: sum-positives : (Listof Real) -> Real) which, using condfoldr, sums only the positive entries in the list
Problem 2
Rewrite the built in function reverse. Hint: you may want to use the append function.
Problem 3
Write the following higher-order function.
- (: f+ : (Number -> Number) (Number -> Number) -> (Number -> Number)) which takes two functions as input and creates a new function that adds the results of the input functions. For example, ((f+ sqr sqrt) 4) is (+ (sqr 4) (sqrt 4)).
Practice
If you still have trouble with using empty, cons, first, rest, and empty? to process lists, you should also do the questions in this assignment using those instead of using build-list, filter, map, foldr, and foldl.
If you still have trouble with using build-list, filter, map, foldr, and foldl to process lists, you should try to do all of the previous homework questions using those instead of empty, cons, first, rest, and empty?.
Submit your work in your repository in hw9/hw9.rkt by noon Monday, July 10.