Assignment #10: Functions as Arguments

Out: Wednesday, March 18th   Due: Thursday, March 26th, 4:00pm

This homework must be done and submitted with your partner. Please see the submissions page for details on how to submit with your partner.



Questions

  1. Problem 19.1.5 from Section 19.1 of How to Design Programs
    Do only the first half of this function - once you test your new functions on the given lists of numbers, the problem is complete. You do not need to improve your function.
  2. Problem 19.2.3 from Section 19.2

    Note: You cannot write the structure definition provided by the text book, because the function pair? was added to the student languages after the text was published. Use a different name for the structure, such as:

    (define-struct pair2 (left right))
    You will receive full credit regardless of what name you choose.

  3. Problem 19.2.4
  4. Problem 21.1.1 from Section 21.1
  5. The following is a definition of the sort function, which we developed in class:
    ;; sort : Listof[Number] -> Listof[Number] ;; to construct a list with all items from alon in descending order (define (sort alon) (local ((define (insert an alon) (cond [(empty? alon) (list an)] [else (cond [(> an (first alon)) (cons an alon)] [else (cons (first alon) (insert an (rest alon)))])]))) (cond [(empty? alon) empty] [else (insert (first alon) (sort (rest alon)))])))
    Design an abstracted version of the sort function which consumes the comparison as an additional argument.
    Use this function to design sort-ascending and sort-descending, which sort a Listof[Number] in ascending and descending order, respectively.
  6. Consider the function find:
    ;; find : Symbol Listof[Symbol] -> Boolean ;; determine if the symbol is in the list (define (find s l) (cond [(empty? l) false] [else (or (symbol=? s (first l)) (find s (rest l)))]))
    Generalize the find function to take an input which is a Question[Symbol] instead of a Symbol
    Then generalize find to handle lists of any kind of data. Does this require any changes to the body of find?