On this page:
1 Trees (29 points)
2 Getting in line (18 points)
3 My dog ate the design recipe (15 points)
4 Bonus Challenge:   Building Built-in Abstractions (8 points)
7.9.0.3

Exam 2

This assignment is due on Monday, March 22 at 9:00pm. Submit each section using Handin as a separate assignment.

1 Trees (29 points)

Submit this section using Handin as assignment exam2-1. Above your answer to each part, put a comment like this:
; Part 1
...
...
 
; Part 2
...
...

Here is the data definition for a binary tree of numbers. A node contains a number and some nodes have two child trees.

; A TreeOfNumbers is one of:
;  - (make-node0 Number)
;  - (make-node2 Number TreeOfNumbers TreeOfNumbers)
 
(define-struct node0 [value])
(define-struct node2 [value first second])
  1. (3 points) Write 3 examples of TreeOfNumbers.

  2. (8 points) Design a function reduce-value that subtracts a given number from the numbers in all the nodes in a TreeOfNumbers.

    ;; reduce-value : TreeOfNumbers Number -> TreeOfNumbers
    ;; subtracts a given number from all the values in a tree
  3. (8 points) Design a function constant-value that takes a TreeOfNumbers and given Number and produces a new TreeOfNumbers with the same structure as the original one, but where all of the values are the given number

    ;; constant-value : TreeOfNumbers Number -> TreeOfNumbers
    ;; set all the values in a tree to the given number
  4. (6 points) Abstract reduce-value and constant-value into a single function modify-value. Provide new definitions reduce-value2 and constant-value2 using modify-value.

  5. (4 points) Use the abstraction modify-value to define a function double-value that doubles all the values in a TreeOfNumbers.

    ;; double-value : TreeOfNumbers -> TreeOfNumbers
    ;; returns a tree similar to input but with all values doubled

2 Getting in line (18 points)

Submit this section using Handin as assignment exam2-2. Above your answer to each part, put a comment like this:
; Part 1
...
...

  1. (2 points) Write three examples of ListOfNumbers, one of which is not sorted from smallest to largest.
    ; A ListOfNumbers is one of:
    ;  - empty
    ;  - (cons Number ListOfNumbers)

  2. (9 points) Design the function insert which takes a Number and an ListOfNumbers and puts the number in the correct place in the list, assuming the list is already sorted. The new number should go before all the numbers bigger than it, and after all the numbers smaller than it.

    ; insert : Number ListOfNumbers -> ListOfNumbers
    ; insert the number in sorted order
    (check-expect (insert 1 empty) (list 1))
    (check-expect (insert 5 (list 1 3 7 9)) (list 1 3 5 7 9))
  3. (7 points) Design the function isort which takes a ListOfNumbers and produces a ListOfNumbers in sorted order.

    Hint! Use insert as a helper function, and follow the template!

    Note: You may not use the built-in sort function to define isort.

3 My dog ate the design recipe (15 points)

Submit this section using Handin as assignment exam2-3. Above your answer to each part, put a comment like this:
; Part 1
...
...

  1. (5 points) The following function was found without a signature, purpose statement, or examples and tests. Recreate them based on the function definition.

    (define (manhattan-distance p1 p2)
      (+ (abs (- (posn-x p1) (posn-x p2)))
         (abs (- (posn-y p1) (posn-y p2)))))
  2. (5 points) The following function was found without a signature, purpose statement, or examples and tests. Recreate them based on the function definition.

    (define (generate-nums n)
      (cond [(zero? n) empty]
            [else (cons (random 10) (generate-nums (sub1 n)))]))
  3. (5 points) The following function was found without a signature, purpose statement, or examples and tests. Recreate them based on the function definition.

    (define (count-matches f? l)
      (cond [(empty? l) 0]
            [(f? (first l)) (+ 1 (count-matches f? (rest l)))]
            [else (count-matches f? (rest l))]))

4 Bonus Challenge: Building Built-in Abstractions (8 points)

Submit this section using Handin as assignment exam2-4. Above your answer to each part, put a comment like this:
; Part 1
...
...

  1. (4 points) Recall the signature and purpose of map and foldr.

    ; [X Y] [X Y -> Y] Y [List-of X] -> Y
    ; applies f from right to left to each item in lx and b
    ; (foldr f b (list x-1 ... x-n)) == (f x-1 ... (f x-n b))
    (define (foldr f b lx) ...)
     
    ; [X Y] [X -> Y] [List-of X] -> [List-of Y]
    ; constructs a list by applying f to each item on lx
    ; (map f (list x-1 ... x-n)) == (list (f x-1) ... (f x-n))
    (define (map f lx) ...)

    Define my-map, which is like map but is defined using foldr.

  2. (4 points) Recall the signature and purpose of foldl.

    ; [X Y] [X Y -> Y] Y [List-of X] -> Y
    ; applies f from left to right to each item in lx and b
    ; (foldl f b (list x-1 ... x-n)) == (f x-n ... (f x-1 b))
    (define (foldl f b lx) ...)
     
    (foldl + 0 '(1 2 3 4 5))
    == (+ 5 (+ 4 (+ 3 (+ 2 (+ 1 0)))))
    == (+ 5 (+ 4 (+ 3 (+ 2 1))))
    == (+ 5 (+ 4 (+ 3 3)))
    == (+ 5 (+ 4 6))
    == (+ 5 10)

    Define the function rev, which reverses the elements of a list of numbers and is defined using foldl.

    ; rev : [List-of Number] -> [List-of Number]
    ; reverse the list
    (check-expect (rev empty) empty)
    (check-expect (rev (list 1 2 3)) (list 3 2 1))