;;; ;;; Part I ;;; ; 1.) (define x 5) (define y 4) (let ((x 3) (z (+ x 1)) ) (* x y z) ) (let* ((x 3) (z (+ x 1)) ) (* x y z) ) (let* ((z (+ x 1)) (x 3) ) (* x y z) ) (list (let ((x 3) (z (+ x 1)) ) (* x y z) ) x y ) ; 2.) (define x 5) (define y 4) (define (foo x) (+ x y)) (define (bar y) (foo y)) (bar 10) ; 3.) (define (flatten tree) (cond ((null? tree) '()) ((pair? tree) (append (flatten (car tree)) (flatten (cdr tree)))) (else (list tree)))) ;;; ;;; Part II. ;;; (define (factorial n) (define (fact-iter n factor) (cond ((>= 0 n) factor) (else (fact-iter (- n 1) (* n factor))))) (fact-iter n 1)) ; f is a two-argument function (define (curry f x) (lambda (y) (f x y))) ;(define g (curry f x)) ;(equal? (g y) (f x y)) (define add5 (curry + 5)) (define add2 (curry + 2)) (add5 10) (add5 2) (add2 10) ; 3.) (define (filter f l) (cond ((null? l) '()) ((f (car l)) (cons (car l) (filter f (cdr l)))) (else (filter f (cdr l))))) (filter number? '(a b 1 3 c 4 d)) (filter symbol? '(a b 1 3 c 4 d)) (filter symbol? '()) ; 4.) (define (positive-only l) (filter (curry < 0) l)) (positive-only '(1 -1 3 2 0 -4 7))