Homework 10

Please read chapter 20 Incremental Refinement, and chapter 21 Refining Interpreters in the textbook.

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)
And include (test) at the bottom of your homework.

You will also need the following batch of data structures:

(define-struct None ())
(define-struct Person
  ([name : String]
   [yob : Integer]
   [father : FT]
   [mother : FT]))
(define-type FT (U None Person))

Problem 1

Write the following functions using the FT structure.

Problem 2

Add the following tree structures to your code:
(define-struct Empty ())
(define-struct Num-Node
  ([n : Number]
   [l : Num-Tree]
   [r : Num-Tree]))
(define-type Num-Tree (U Empty Num-Node))

(define-struct Str-Node
  ([n : String]
   [l : Str-Tree]
   [r : Str-Tree]))
(define-type Str-Tree (U Empty Str-Node))

(define-struct (Node A)
  ([n : A]
   [l : (Tree A)]
   [r : (Tree A)]))
(define-type (Tree A) (U Empty (Node A)))
To make sure you understand the structures, I suggest first making sure that you can draw the following tree on paper:
(Num-Node 4 (Num-Node 2 (Empty) (Empty))
          (Num-Node 8 (Num-Node 7 (Num-Node 6 (Empty) (Empty)) (Empty)) (Empty)))
Use the above structures to write the following five functions:

Problem 3

In this part, you will write your own higher-order functions, similar to the built-in map, foldr, or filter functions. To implement them, write recursive functions. You may use the built-in map, filter, or folding functions, but are not required to do so. After you implement each, you will then use them to perform a task that cannot be easily accomplished with the built-in ones.

Practice

Please be sure to practice with lambda and match. You are seldom required to use them, but they will be on the quiz. Also be sure to practice with map, filter, fold, and build-list: they will also be on the quiz.

Submit your work in your repository in hw10/hw10.rkt by noon Wednesday, July 12.