exercise-14.scm (2878B)
1 (define-library (sicp tests chapter-4 exercise-14) 2 (import (scheme base) 3 4 (srfi :64) 5 6 (sicp solutions chapter-4 exercise-14) 7 8 (sicp utils)) 9 10 (begin 11 ;; (debug! #t) 12 13 ;; TODO: Not sure if this is the real solution. 14 15 (test-begin "chapter-4-exercise-14") 16 17 (test-equal 1 18 (eval '(begin 19 (define (map f l) 20 (cond 21 ((null? l) 22 '()) 23 (else 24 (cons (f (car l)) 25 (map f (cdr l)))))) 26 (car (map (lambda (x) (+ x 1)) 27 '(0 1 2)))) 28 (setup-environment))) 29 30 (test-equal -1 31 (eval '(begin 32 (define (map f l) 33 (cond 34 ((null? l) 35 '()) 36 (else 37 (cons (f (car l)) 38 (map f (cdr l)))))) 39 (car (map (lambda (x) (- x 1)) 40 '(0 1 2)))) 41 (setup-environment))) 42 43 (test-equal '(2 3) 44 (eval '(begin 45 (define (map f l) 46 (cond 47 ((null? l) 48 '()) 49 (else 50 (cons (f (car l)) 51 (map f (cdr l)))))) 52 (cdr (map (lambda (x) (+ x 1)) 53 '(0 1 2)))) 54 (setup-environment))) 55 56 (test-equal '() 57 (eval '(map-form (lambda (x) (+ x 2)) '()) 58 (setup-environment))) 59 60 (test-equal '(3 4 5) 61 (eval '(map-form (lambda (x) (+ x 2)) (list 1 2 3)) 62 (setup-environment))) 63 64 (test-equal '(3 4 5) 65 (eval '(let ((l (list 1 2 3))) 66 (map-form (lambda (x) (+ x 2)) l)) 67 (setup-environment))) 68 69 (test-equal 1 70 (eval '1 71 (setup-environment))) 72 73 (test-equal 1 74 (eval '(begin 75 (define a 1) 76 1) 77 (setup-environment))) 78 79 (test-equal 1 80 (eval '(begin 81 (define a 1) 82 (let ((b 1)) 83 b)) 84 (setup-environment))) 85 86 (test-equal 1 87 (eval '(begin 88 (define a 1) 89 (let* ((b 1)) 90 b)) 91 (setup-environment))) 92 93 (test-equal 2 94 (eval '(begin 95 (define a 1) 96 (+ a 1)) 97 (setup-environment))) 98 99 (test-equal 3 100 (eval '(begin 101 (define a 1) 102 (let ((b (+ a 1))) 103 (+ a b))) 104 (setup-environment))) 105 106 (test-equal 5 107 (eval '(begin 108 (define a 1) ;; a = 1 109 (let ((b (+ a 1))) ;; b = 1 + 1 = 2 110 (set! b (+ b b)) ;; b = b + b = 2 + 2 = 4 111 (+ a b))) ;; a = a + b = 1 + 4 = 5 112 (setup-environment))) 113 114 (test-end "chapter-4-exercise-14") 115 ))