Problem | 0 | 1 | 2 | 3 | 4 | 5 | 6 | Total |
Grade | 2 | 17 | 17 | 17 | 17 | 10 | 17 | 95 |
a) (bind ((x 10)(f +)) (bind ((x (f x x))(y (f x x))) (f y y))) =>
b)
(bind ((y 20)) (bind ((x y) (f (method (x) (+ x y)))) (f x))) =>
c)
(head (tail (head (tail '(1 (2 (4)) 5))))) =>
d)
((method (f) (f f)) (method (g) (g g))) =>
e)
(+ ((method (x) x) 5) 4) =>
(17 Points) In this problem we consider a procedure for computing all
pairs of the elements in a list. For a list (en...e1)
we define the pairs to be a list containing all pairs (ei, ej),
for i > j and 1 <= i,j <= n. We choose to represent
each pair as a Dylan <pair>
. For
example, here are some lists with corresponding lists of pairs:
(a) -> ()
(a b) -> ((a b))
(a b c) -> ((a b) (a c) (b c))
The following procedure is purported to compute the unordered pairs for a given list, lst:
(define all-pairs (method ((lst <list>)) (cond ((null? (cdr lst)) ()) ((null? (cddr lst)) (list lst)) (else (append (pair-with-list (car lst) (cdr lst)) (all-pairs (cdr lst)))))))
where cddr is defined as
(define cddr (compose cdr cdr)) (define compose (method ((f <function>) (g <function>)) (method ((x (<object>))) (f (g x)))))
All-pairs makes use of the helper procedure
(define pair-with-list (method ((elt <object>) (lst <list>)) (if (null? lst) () (cons (list elt (car lst)) (pair-with-list elt (cdr lst))))))
Prove by induction that (all-pairs lst) produces a list containing all the pairs for any lst of length n>1, using the above definition of pairs. You may assume that
(pair-with-list x (en ... e1))
returns the list ((x en) ... (x e1)), for any length n. You may further assume that (append l1 l2) correctly forms the concatenation of the lists l1 and l2, for any length lists.
(define map (method ((f <function>) (lst <list>)) (if (null? lst) () (cons (f (car lst)) (map f (cdr lst))))))
(define (deriv <function>) (method ((f <function>) (dx <number>)) (method ((x <number>)) (/ (- (f (+ x dx)) (f x)) dx))))
Deriv-list should also take two arguments: the list and the accuracy.
(define accumulate (method ((base <object>) (combiner <function>) (lst <list>)) (if (null? lst) base (combiner (car lst) (accumulate base combiner (cdr lst))))))
Rewrite the map procedure in terms of accumulate (i.e., define the map procedure using accumulate).
(cons 1 (cons 2 (cons 3 ()))) -> (1 2 3) (list 1 2 3) -> (1 2 3) (cons 1 (cons 2 3)) -> (1 2 . 3) (list (cons 1 2) 3 (list 4 5)) -> ((1 . 2) 3 (4 5))
(1 2 . 3)
((1 . 2) 3 (4 5))
(define all-pairs (method ((lst <list>)) (cond ((null? (cdr lst)) ()) ((null? (cddr lst)) (list lst)) (else: (append (pair-with-list (car lst) (cdr lst)) (all-pairs (cdr lst)))))))
making use of the helper procedure
(define pair-with-list (method ((elt <object>) (lst <list>)) (if (null? lst) () (cons (list elt (car lst)) (pair-with-list elt (cdr lst))))))
(define adjoin-set (method ((x <object>) (set <>)) (cond ((nul? set) (list x)) ((= x (car set)) set) ((< x (car set)) (cons x set)) (else: (cons (car set) (adjoin-set x (cdr set))))))) (define element-of-set? (method ((x <object>) (set <list>)) (cond ((null? set) \#f) ((= x (car set)) \#t) ((< x (car set)) #f) (else: (element-of-set? x (cdr set))))))
Write a linear time implementation of intersection-set given this new ordered representation of sets (i.e., for two sets of length m and n your procedure should run in time O(m+n).)