C311 Fall 1996 -- Assignment 5. Sample scheme interaction
Chez Scheme Version 5.0b
Copyright (c) 1994 Cadence Research Systems
> (load "a5.ss")
;;
;; testing letrec
;;
> (run '(letrec ([odd? (lambda (n)
(if (zero n)
0
(even? (sub1 n))))]
[even? (lambda (n)
(if (zero n)
1
(odd? (sub1 n))))])
(list (odd? 0) (even? 0) (even? 1) (odd? 1) (odd? 2) (even? 2) (even? 3))))
(0 1 0 1 0 1 0)
> (run '(letrec ([fact (lambda (n)
(if (zero n)
1
(* n (fact (sub1 n)))))])
(list (fact 4) (fact 5) (fact 6))))
(24 120 720)
> (run '(letrec ([map (lambda (f ls)
(if (null ls)
emptylist
(cons (f (car ls)) (map f (cdr ls)))))])
(map add1 (list 11 13 17 19))))
(12 14 18 20)
> (run '(let ([ls (list 11 13 17 19)])
(letrec ([map (lambda (f ls)
(if (null ls)
emptylist
(cons (f (car ls)) (map f (cdr ls)))))])
(map (lambda (n) (cons n ls)) ls))))
((11 11 13 17 19)
(13 11 13 17 19)
(17 11 13 17 19)
(19 11 13 17 19))
;;
;; testing delta (i'm using the letrec test cases, as you can see)
;;
> (run '(let ([odd? (delta (n)
(if (zero n)
0
(even? (sub1 n))))]
[even? (delta (n)
(if (zero n)
1
(odd? (sub1 n))))])
(list (odd? 0) (even? 0) (even? 1) (odd? 1) (odd? 2) (even? 2) (even? 3))))
(0 1 0 1 0 1 0)
> (run '(let ([fact (delta (n)
(if (zero n)
1
(* n (fact (sub1 n)))))])
(list (fact 4) (fact 5) (fact 6))))
(24 120 720)
> (run '(let ([map (delta (f ls)
(if (null ls)
emptylist
(cons (f (car ls)) (map f (cdr ls)))))])
(map add1 (list 11 13 17 19))))
(12 14 18 20)
> (run '(let ([ls (list 11 13 17 19)])
(let ([map (delta (f ls)
(if (null ls)
emptylist
(cons (f (car ls)) (map f (cdr ls)))))])
(map (delta (n) (cons n ls)) ls))))
((11 11 13 17 19) (13 13 17 19) (17 17 19) (19 19))
;;
;; testing "strange"
;;
; replace is a procedure to help me test your strange function.
; it replaces a symbol with a new one:
; (replace 'a 'b '((a b c) a)) ==> ((b b c) b)
> (define replace
(lambda (old new exp)
(cond
[(null? exp) '()]
[(pair? exp) (cons
(replace old new (car exp))
(replace old new (cdr exp)))]
[(eq? old exp) new]
[else exp])))
> (define with-dynamic-scope (run (replace 'lambda 'delta strange)))
> (define with-both-scopes (run strange))
> (define with-static-scope (run (replace 'delta 'lambda strange)))
> (equal? with-dynamic-scope with-static-scope)
#f
> (equal? with-dynamic-scope with-both-scopes)
#f
> (equal? with-static-scope with-both-scopes)
#f
Last modified on Mon Oct 7 00:16:36 1996