CISC 280 review sheet for second midterm exam, Tuesday, April 16, 2002
Exam covers all of chapter 2 except sections 2.1.3 (data as procedure) and 2.3.3 (representing sets).
[However, you are encouraged to read and think about these sections for your own edification!]
sample questions
-
What is a recognizer? __________
-
What is a constructor? __________
-
What is a proper list? __________
-
What is a generic operator? __________
The following apply to the lists
L = ((1 2) (3 4)) and M = (5 6 7).
-
(append L M) returns __________.
-
(list L M) returns __________.
-
(cons L M) returns __________.
-
(cons (list L (+ 1 3)) (append (list M) M)) returns __________.
-
(cdr (car L)) returns __________.
-
(length (list L M L L M)) returns __________.
-
Write (reverse L), the procedure that returns a list which has the items
of L in it, but in reverse order.
-
Write a procedure split which takes a list as argument
and returns a list of length 2. The two items in this output list
are lists. The first one is the list of all the elements of the input
list that are in odd numbered positions. The second list is all the
elements in even numbered positions.
> (split (list 1 2 3 4 5 6 7 8 9 0)) ; input may have even length
---> ((1 3 5 7 9) (2 4 6 8 0))
> (split (list 1 2 3)) ; input may have odd length
---> ((1 3) (2))
> (split (list 1)) ; input may be short
---> ((1) ())
> (split '() ) ; input may be very short
---> (() ())
> (split (list "a" 1 "b" 77 (* 2 4) "c" '(1 2))) ; it doesn't matter what's in the list
---> (("a" "b" 8 (1 2)) (1 77 "c"))
- In the following code, join-em is a procedure which (like append)
takes two lists and forms the list consisting of all the elements in the first
list followed by all the elements in the second list. What difference
does it make in the number of steps to do tree->list on a tree T with
n nodes if join-em is a one step procedure or is a k step procedure,
where k is the number of items in the first list?
(define (tree->list T)
(if (empty-tree? T)
'()
(join-em (tree->list (left-branch T))
(cons (node-value T)
(tree->list (right-branch T)) )) ))
Remark: A one step procedure would be possible if lists were doubly linked circular
structures rather than Scheme's singly linked lists. This is one benefit of doubly linked
lists. There are also drawbacks, of course, such as using more memory.
-
Mercy hospital and the VA hospital have decided to use a common system
to support their patient records. Each hospital keeps a record on new
patients consisting of the patient's name, date of admission, and diagnosis.
However, Mercy uses a list of the form (name date diag), whereas
VA uses (diag name date). In each hospital, many programs deal with these
records, so it is not possible to simply change one to conform to the other.
To handle the combined record keeping system, it is necessary to provide
the constructors make-Mercy-record and make-VA-record and provide
the generic selectors name, date, and diag.
Sketch how you might use data-directed programming to do this,
using manifest type and using put and get on a table of procedures.
Stuff on painters
- Write a painter named en to draw the letter "N" with its vertical
bars ON the side edges of the frame.
- What does this-painter draw?
(define this-painter (beside (beside en (flip-vert en)) en) )
- Define a procedure (mirror p:Painter) that
produces a painter
that draws p twice, inverted, flip-vert, in the top half and unchanged
in the bottom half of the frame.
- Define a procedure (circus-mirror u:UPO p:Painter) that
produces a painter that draws p twice, once with
u applied to p in the top half, and once, unchanged, in the bottom half
of the frame. (UPO means unary painter operator.)
- Define a higher order procedure (new-mirror u:UPO) that
produces a UPO so that ((new-mirror u) p) is a painter that draws
the same thing as (circus-mirror u p).
Here is a quick summary of some procedures studied and other terminology,
Pairs: car cdr cons pair? null?
Uses of pairs
abstraction barriers - levels from basic scheme up to application functions
Sequences (lists): nth length list append reverse list?
Symbols and quote '
Symbolic differentition: Expressions are naturally processed using tree recursion.
Simplification is an issue that Make-sum, etc can handle.
rational numbers:
interval arithmetic:
List processing:
(map f L) (filter p? L)
More list processing:
(flatten L) (extent L)
Painters: segments->painter,
Painter ops: transform-painter,
unary ops: flip-vert, rotate90, erase, etc.
binary ops: beside, below,
higher order ops: square-of-four,
recursive ops: right-split, corner-split, up-split, square-limit,
Frames: constructors and selectors
Multiple representations,
Manifest type,
Data directed programming
Some good exercises from chapter 2 are: 2.18, 2.21, 2.22, 2.23, 2.24