; ===Problem 3 Tests=== even? ; expect #[even?] append ; expect #[append] print ; expect #[print] / ; expect #[/] adder? ; expect Error: unknown identifier: adder? ; ===Problem 4 Tests=== (+ 1 5) ; expect 6 (* 2 4 (- 4 1) 4) ; expect 96 (even? 4991) ; expect False (null? 2) ; expect False (boolean? True False) ; expect Error (odd? (* (+ 234 1) 33)) ; expect True (1 2 3 4 5) ; expect Error: cannot call: 1 (cons 1 2) ; expect (1 . 2) (1 . 2) ; expect Error: malformed list: (1 . 2) (null? (cdr (cdr (list 1 2)))) ; expect True ; ===Problem 5A Tests=== (define x (- (define y 44) 41)) ; expect Error: (define x (- (eval(define y 44)) 41)) ; expect x x ; expect 3 (define z (cons 2 3)) ; expect z z ; expect (2 . 3) (define 42 x) ; expect Error: Non-symbol: 42 ; ===Problem 6B Tests=== (define 'pineapple 'tasty) ; expect quote (define x (cons 'cdr '('(3 4)))) ; expect x x ; expect (cdr (quote (3 4))) (define y '1234) ; expect y y ; expect 1234 ; ===Problem 7 Tests=== (begin (define x 10) (define y 10) (define zapplepear (* y x)) z) ; expect 100 (begin (print 4) (print nil) (print applepear)) ; expect okay (begin 3 4 5 6 7 3 2 2 1 5) ; expect 5 ; ===Problem 8 Tests=== (lambda () (+ 1 2)) ; expect (lambda () (+ 1 2)) (lambda (x) (lambda (y) (+ x y))) ; expect (lambda (x) (lambda (y) (+ x y))) (lambda (x y z) ((* 5 x y z) (print 2) (= 3 3))) ; expect (lambda (x y z) ((* 5 x y z) (print 2) (= 3 3))) (define x (lambda () (lambda (d) (print d)))) ; expect x x ; expect (lambda () (lambda (d) (print d))) ; ===Problem 9 Tests=== (define (g x) ((* x 2) (= 6 x))) ; expect g g ; expect (lambda (x) ((* x 2) (= 6 x))) (define (make-adder x) (define (adder y) (+ y x) ) adder ) ; expect make-adder (eval 'make-adder) ; expect (lambda (x) (define (adder y) (+ y x)) adder) (make-adder 3) ; expect (lambda (y) (+ y x)) ((make-adder 3) 4) ; expect 7 ; ===Problem 10 Tests=== (define (berry straw) (define (smallberry black) (>= straw black) ) smallberry ) ; expect berry ((berry 3) 4) ; expect False ; ===Problem 11B Tests=== (lambda (x y z) (+ x y)) ; expect (lambda (x y z) (+ x y)) (lambda (x y) (lambda (x y) y)) ; expect (lambda (x y) (lambda (x y) y)) (lambda (x y x) x + x) ; expect Error: (define (f 1 2) 1) ; expect Error: (define (f 'q) q) ; expect Error: (define (f q) q) ; expect f ; ===Problem 12 Tests=== (define (f y) (begin (/ y 10))) ; expect f (f 2 3) ; expect Error ((lambda (a b c) (/ (+ a b c) 3)) 3 4 5) ; expect 4 (define (g fn fn2 x) (fn (fn2 x)) ) ; expect g (g f (lambda (a) (* a a)) 3) ; expect 0.9 ; ===Problem 13 Tests=== (if 0 1 3) ; expect 1 (if nil 'tree 'root) ; expect tree (if False (/ 1 0) (cons 1 (cons 2 nil))) ; expect (1 2) (define (filter f lst) (if (null? lst) nil (if (f (car lst)) (cons (car lst) (filter f (cdr lst))) (filter f (cdr lst)) )) ) ; expect filter (filter (lambda (x) (= (remainder x 3) 0)) (list 0 1 2 3 4 5 6 7 8 9)) ; expect (0 3 6 9) (if (begin (even? 33) (odd? 23) (null? 2) (> 4 3)) (begin (list 2 3 4 5) (list 2 3 4)) (list 2 3 4 5 6)) ; expect (2 3 4) (if (>= 2 3) '(1. 2) (define (f x y) (lambda () (+ x y)))) ; expect f ((f 1 2)) ; expect 3 (if 'q 2 3) ; expect 2 ; ===Problem 14 Tests=== (if (and (or False True False) 0 2 False (/ 1 0)) '(a b c d) '(e f g h)) ; expect (e f g h) (and) ; expect True (or) ; expect False (or True False 1) ; expect True (and True 0 3 1 nil) ; expect () (define (nodots lst) nil) ; expect nodots (or False (nodots '()) nodots) ; expect () (or (and 3 0 3 2 False) (and 2 3 1)) ; expect 1 ; ===Problem 15 Tests=== (define (letter score) (cond ((>= score 90) 'A) ((>= score 80) 'B) ((>= score 70) 'C) ((>= score 60) 'D) (else 'F) ) ) ; expect letter (letter 62) ; expect d (cond ((null? nil) (if (null? 1) 3 (if (>= 5 4) 7))) (else (list 1 2 3 4)) ) ; expect 7 (cond ((<= 3 2) (+ 3 4)) (else (* 3 4)) ) ; expect 12 (cond (nil)) ; expect () (cond ((- 0 1) (cons 1 2)) (else (cons 1 3)) ) ; expect (1 . 2) (cond (True (define (add x) (+ x 4)) (define (sub x) (- x 1)) (define (cube x) (* x x x)) (add (sub (cube 3)))) (else 2) ) ; expect 30 ; ===Problem 16 Tests=== (let ((x 332) (y 1) (z (+ x y))) (+ z 2)) ; expect Error: unknown identifier: x (let ((x 332) (y 1) (z 1)) (+ z 2 x)) ; expect 335 (let ((1 'hello) (y 2) ) (+ x y)) ; expect Error: (let ((x 'hello) (y 2) ) (cons x y)) ; expect (hello . 2) (define x 15) ; expect x (let (x 23) x) ; expect Error: badly formed expression: x (let ((x 23)) x) ; expect 23 x ; expect 15 (let ((x (let ((y 2)) y))) x) ; expect 2 ; ===Problem 17=== (define x (mu (a) (+ 2 b))) ; expect x (define (y fn b) (fn 2)) ; expect y (y x 22) ; expect 24 b ; expect Error: unknown identifier: b (define (z fn c) (fn 5)) ; expect z (z x 22) ; expect Error: unknown identifier: b (x 22) ; expect Error: unknown identifier: b (define x (mu () (lambda (y) (mu () (+ c y))))) ; expect x (define (z c y) (((x) 5))) ; expect z (z 4 3) ; expect 7
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question