Problem Set 4
Language: Beginning Student with List Abbreviations
Due: 11:59 PM Thursday, Feb 2.
1 Sorted lists
| ; A sorted-list is either |
| ; - empty |
| ; - (cons number[n] sorted-list[l]) |
| ; INVARIANT: each number in `l' is greater than `n' |
Design the following function.
| ; in? : sorted-list number -> boolean |
| ; determines if `n' is in `l' |
| (define (in? n l) |
| ---) |
You must exploit the sorted-list invariant.
2 Binary search trees
| ; a person is |
| ; (make-person number symbol) |
| (define-struct person (ssn name)) |
| ; A database is either |
| ; - empty |
| ; - (make-db-node person[p] database[l] database[r]) |
| (define-struct db-node (person left right)) |
| ; INVARIANT: |
| ; every person in `l' has a smaller SSN than `p', and |
| ; every person in `r' has a larger SSN than `p' |
Design the function inorder. It consumes a database and produces a list of all the SSNs in the database. The list contains the SSNs in ascending order.
You must exploit the database invariant.
Design the function create-database. It consumes a database d and a person p. It produces a database that is just like d but contains the following db-node structure in place of the appropriate empty database:
Design the function create-database-from-list. It consumes a list of persons; it produces a database by repeatedly applying create-database.