forked from cisco/ChezScheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscons.ss
41 lines (31 loc) · 1.1 KB
/
scons.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
;;; scons.ss
;;; a stream-construction facility
;;; The scons special form performs a cons, suspending the cdr field
;;; by enclosing it in a procedure of no arguments. scdr tests to see
;;; if the cdr is a procedure, and if so, invokes it. scar is provided
;;; for symmetry; it is just car.
;;; The function stream-ref is simply list-ref defined in terms of
;;; scdr and scar.
;;; factlist and fiblist are two infinite streams.
;;; Try (stream-ref factlist 10) or (stream-ref fiblist 20).
;;; scons could easily suspend the car field as well. This would
;;; implement the lazy cons of Friedman & Wise.
(define-syntax scons
(syntax-rules ()
((_ car cdr) (cons car (lambda () cdr)))))
(define scar car)
(define scdr
(lambda (x)
(when (procedure? (cdr x)) (set-cdr! x ((cdr x))))
(cdr x)))
(define stream-ref
(lambda (x n)
(if (zero? n)
(scar x)
(stream-ref (scdr x) (1- n)))))
(define factlist
(let fact ([a 1] [n 1])
(scons a (fact (* a n) (1+ n)))))
(define fiblist
(let fib ([fib-2 0] [fib-1 1])
(scons fib-1 (fib fib-1 (+ fib-2 fib-1)))))