All of these problems should be done without using side effects (i.e. redefining variables or using set!). Be sure to test your functions on various cases, including empty lists, simple lists with no sublists, complex lists, etc.
(define a 21) (define b 100) (define (clam a) (- a b)) (define (squid b) (clam b) )What are the values of the following scheme expressions?
(let ( (b 12) (c (+ b 7)) ) (* a b c) )
(let* ( (b 12) (c (+ b 7)) ) (* a b c) )
(let* ( (a (+ b a)) (b 12) (c (+ b a)) ) (* a b c) )
(squid 11)
(define w 42) (define x 17) (define y (lambda (n) (+ n w))) (define z (lambda (x) (+ x 2)))What are the values of the following expressions?
(map (lambda (x) (z x)) '(1 2 3 4))
(let ( (w (map y '(1 2 3 4)))) w)
(define (sumlist x) (if (null? x) 0 (+ (car x) (sumlist (cdr x)))))
(define (positive-numbers x) (cond ((null? x) ()) ((> (car x) 0) (cons (car x) (positive-numbers (cdr x)))) (else (positive-numbers (cdr x)))))
(define (r2 x) (let ((r (reverse x))) (append r r)))
(define (same-length x y) (cond ((and (null? x) (null? y)) #t) ((null? x) #f) ((null? y) #f) (else (same-length (cdr x) (cdr y)))))
You will be asked to turn in both the function definitions themselves and a printout showing a Scheme session where you test the functions. Your test cases should be sufficient to show that the functions work correctly (i.e., works on both empty and non-empty lists, etc.) In DrScheme, you can use File->Print Interactions to print the session transcript.
(deep-map op lst)
that applies function op
to list lst
. deep-map
returns an object with the same "shape" as lst
,
with each element being the result of applying op
to the corresponding element of the original component of lst
.
If elements of lst
are themselves
lists, they should be examined recursively and op
should be applied to those elements. Do NOT use the Scheme
function
map
in your implementation.
NOTE: a function applied to the empty list should return the empty list.
Examples:
(define (double x) (* 2 x))
(deep-map double '(1 (2) 3 4 () )) => (2 (4) 6 8 () )
(deep-map number? '(() ((3 g) a) 34 (g 4) ))) => (() ((#t #f) #f) #t (#f #t))
deep-double
that
uses deep-map
to apply the
function double
(see definition of double
in the
previous example) to every item in its argument:
(deep-double '(1 (2) 3 4 )) => (2 (4) 6 8)
(deep-double '(((1) 2) 3))) => (((2) 4) 6)
deep-dbl
to do
the same thing using a lambda expression instead of the named double
function.(zip op x y)
that takes lists (x1 x2 x3 ....)
and (y1 y2 y3 ...)
and returns
the list ((op x1 y1) (op x2 y2) (op x3 y3) ...)
. If either
list argument x
or y
is empty, zip
should
return an empty list. Examples:(zip + '(1 2 3 4) '(5 10 15 20)) => (6 12 18 24)
(zip list '(a b c d) '(6 7 8 9)) => ((a 6) (b 7) (c 8) (d 9))
(3fold op id x y)
,
where op
is a function, id
is the identity element for that function, x
is the list (x1 x2 x3 ...)
and y
is the list (y1 y2 y3 ...)
.
Execution of (3fold op id x y)
should return the list
(op x1 y1 (op x2 y2 (op x3 y3 (...(op xn yn id))))In other words,
3fold
reduces the two lists to a single value by applying function op
to
pairs of values taken from x
and y
and the result of
recursively evaluating 3fold
on the tails of the lists. The
identity id
is the value of function 3fold
for the
base case where either list is empty. Examples:(3fold + 0 '(1 2 3) '(10 15 20)) => 51
(3fold * 1 '(-1 2 -3) '(2 -10 -3)) => 360
(3fold list () '(a b c) '(d e f)) => (a d (b e (c f ())))
(3fold - 0 '(10 5 3) '(5 10 10)) => 3