This exam contains 10 problem worth between 5 and 15 points each. There are a total of 100 points on this exam. Please make sure you have all 4 pages and all 10 problems. Please put your name on the first page you hand in, and your initials on all other pages. Please staple the pages together before handing them in.
a. ((4 5) (3 9))
b. (17 ((9)) . 5)
: (define smudge 18)
: (define joon (cons 3 4))
: (define (hugin smudge pinky)
(lambda (simone)
(set! smudge (- smudge simone))
(set-car! pinky (- smudge simone))
(+ (car pinky) (cdr pinky))))
: (define simone (hugin smudge joon))
: (simone 5)
????? BLANK A ?????
: joon
????? BLANK B ?????
: smudge
????? BLANK C ?????
: (expand-let '(let ((x 2)
(y 1))
(+ x y)))
should return
((lambda (x y) (+ x y)) 2 1).Write a procedure expand-let that takes as input a Scheme let expression, and returns the equivalent procedural form in which the top-level let has been eliminated. You may assume that the input is a valid let expression.
(decode bits huffman-tree).
This procedure takes two arguments: bits is a list
of 0's and 1's, and huffman-tree is a huffman tree.
The procedure should return the list of symbols encoded
by bits using the huffman tree.
You need not do any error checking.
You may assume the following code is defined already:
;; representing huffman trees
(define (make-leaf symbol weight)
(list 'leaf symbol weight))
(define (leaf? object)
(eq? (car object) 'leaf))
(define (symbol-leaf x) (cadr x))
(define (weight-leaf x) (caddr x))
(define (make-code-tree left right)
(list left
right
(append (symbols left) (symbols right))
(+ (weight left) (weight right))))
(define (left-branch tree) (car tree))
(define (right-branch tree) (cadr tree))
(define (symbols tree)
(if (leaf? tree)
(list (symbol-leaf tree))
(caddr tree)))
(define (weight tree)
(if (leaf? tree)
(weight-leaf tree)
(cadddr tree)))
huffman-tree,
huffman-tree, and
bits.
(decode bits huffman-tree) to run?
make-monitored that takes as input a procedure
f of one input. The result returned by
make-monitored is a procedure of one argument, say
mf, that keeps track of the number of times it has been
called by maintaining an internal counter. If the input to
mf is the special symbol how-many-calls?,
then mf returns the value of the counter. If the
input is the special symbol reset-count, then
mf resets
the counter to zero. For any other input, mf
returns the result of calling f on that input and
increments the counter. For example:
: (define s (make-monitored sqrt))
: (s 100)
10
: (s 'how-many-calls?)
1
mul-streams, analagous
to the add-streams procedure we saw in class,
which takes two streams as arguments and
returns the stream consisting of the element-wise
product of its two input streams.
: (define factorials
(s-cons 1
(mul-streams ?? BLANK A ???>
?? BLANK B ???>)))
: (define y 5)
: (define (foo x)
(set! x 3)
x)
: (foo y)
3
: y
5
Notice that the value of y is unchanged.
In some cases, it is desirable for the value of y (or whatever
variable is passed in a call to "foo") to be altered
by "foo". In this case, the argument to "foo" is called a
variable parameter
You are to write a data structure that will implement this:
Define a constructor
(make-vp x)
("vp" stands for variable-parameter, "x" is a number),
a selector
(value vp),
and a mutator
(set-vp! vp y) (this sets "vp" to "y")
to implement variable parameters.
For example, your data structure would allow the following
interaction:
: (define y (make-vp 5))
: (value y)
5
: (define (foo vp)
(set-vp! vp 3)
(value vp))
: (foo y)
3
: (value y)
3
(cycle? x)
that detects if there is any cycle whatsoever in list structure
x. The cycle may involve taking CARs, CDRs, or
any combination of them.