1 | [[tags: egg]] |
---|
2 | |
---|
3 | == srfi-40 |
---|
4 | |
---|
5 | [[toc:]] |
---|
6 | |
---|
7 | === Description |
---|
8 | |
---|
9 | An implementation of [[http://srfi.schemers.org/srfi-40/|SRFI 40]], |
---|
10 | ''A Library of Streams''. Streams are a way of representing and |
---|
11 | computing with infinite sequences. In addition to providing standard |
---|
12 | library procedures for working with streams, this implementation includes |
---|
13 | and is based on new definitions for the lazy evaluation primitives |
---|
14 | {{delay}} and {{force}} and a new primitive called {{lazy}}. |
---|
15 | These definitions have the advantage over the traditional ones of being |
---|
16 | safe-for-space. For details see [[http://srfi.schemers.org/srfi-45/|SRFI 45]], |
---|
17 | ''Primitives for expressing iterative lazy algorithms''. |
---|
18 | |
---|
19 | Note also that SRFI 40 specifies even streams rather than the odd |
---|
20 | streams traditionally used in Scheme and found in books like |
---|
21 | ''Structure and Interpretation of Computer Programs''. See the text |
---|
22 | of the SRFI for what this means and the implications. |
---|
23 | |
---|
24 | Using this extension does not modify the native bindings for {{delay}} |
---|
25 | and {{force}} as the new versions are encapsulated within this |
---|
26 | implementation. |
---|
27 | |
---|
28 | === Author |
---|
29 | |
---|
30 | Based on the original SRFI 40 reference implementation by Philip |
---|
31 | L. Bewig and Andre von Tonder, modified for Chicken by Category 5. |
---|
32 | |
---|
33 | === Requirements |
---|
34 | |
---|
35 | None |
---|
36 | |
---|
37 | === Download |
---|
38 | |
---|
39 | [[http://www.call-with-current-continuation.org/eggs/9p.egg|9p.egg]] |
---|
40 | |
---|
41 | === Documentation |
---|
42 | |
---|
43 | SRFI 40 provides the following values and procedures: |
---|
44 | |
---|
45 | <constant>stream-null</constant> |
---|
46 | |
---|
47 | The distinguished null stream. |
---|
48 | |
---|
49 | <procedure>(stream? object)</procedure> |
---|
50 | |
---|
51 | {{#t}} if {{object}} is a stream, {{#f}} otherwise. |
---|
52 | |
---|
53 | <procedure>(stream-cons object stream)</procedure> |
---|
54 | |
---|
55 | The primitive stream constructor. |
---|
56 | |
---|
57 | <procedure>(stream-null? object)</procedure> |
---|
58 | |
---|
59 | {{#t}} if {{object}} is the null stream, {{#f}} otherwise. |
---|
60 | |
---|
61 | <procedure>(stream-pair? object)</procedure> |
---|
62 | |
---|
63 | {{#t}} if {{object}} is a non-null stream, {{#f}} otherwise. |
---|
64 | |
---|
65 | <procedure>(stream-car stream)</procedure> |
---|
66 | |
---|
67 | First element of stream. |
---|
68 | |
---|
69 | <procedure>(stream-cdr stream)</procedure> |
---|
70 | |
---|
71 | Remaining elements of stream after the first. |
---|
72 | |
---|
73 | <procedure>(stream-delay object)</procedure> |
---|
74 | |
---|
75 | SRFI 40 {{delay}}. |
---|
76 | |
---|
77 | <procedure>(stream object ...)</procedure> |
---|
78 | |
---|
79 | Returns a new stream whose elements are {{object ...}}. |
---|
80 | |
---|
81 | <procedure>(stream-unfoldn generator seed n)</procedure> |
---|
82 | |
---|
83 | Returns n+1 streams from {{(generator seed)}}. |
---|
84 | |
---|
85 | <procedure>(stream-map proc stream ...)</procedure> |
---|
86 | |
---|
87 | Returns the stream produced by applying {{proc}} to each element of {{stream}}. |
---|
88 | |
---|
89 | <procedure>(stream-for-each proc stream ...)</procedure> |
---|
90 | |
---|
91 | Applies {{proc}} to each element of {{stream}} for side-effects. |
---|
92 | |
---|
93 | <procedure>(stream-filter pred? stream)</procedure> |
---|
94 | |
---|
95 | Returns a new stream consisting of the elements of {{stream}} for |
---|
96 | which {{pred?}} returns true. |
---|
97 | |
---|
98 | === Examples |
---|
99 | |
---|
100 | <examples><example> |
---|
101 | <expr> |
---|
102 | (define stream-ref |
---|
103 | (lambda (s n) |
---|
104 | (let loop ((s s) (i 0)) |
---|
105 | (if (= i n) (stream-car s) |
---|
106 | (loop (stream-cdr s) (+ i 1)))))) |
---|
107 | (define stream-take |
---|
108 | (lambda (n s) |
---|
109 | (if (= n 0) '() |
---|
110 | (cons (stream-car s) |
---|
111 | (stream-take (- n 1) (stream-cdr s)))))) |
---|
112 | |
---|
113 | (define integers-from |
---|
114 | (lambda (n) |
---|
115 | (stream-cons n (integers-from (+ n 1))))) |
---|
116 | (define integers (integers-from 0)) |
---|
117 | |
---|
118 | (define divisible? (lambda (n k) (zero? (modulo n k)))) |
---|
119 | (define sieve |
---|
120 | (lambda (s) |
---|
121 | (stream-cons (stream-car s) |
---|
122 | (sieve |
---|
123 | (stream-filter |
---|
124 | (lambda (n) (not (divisible? n (stream-car s)))) |
---|
125 | (stream-cdr s)))))) |
---|
126 | (define primes (sieve (integers-from 2))) |
---|
127 | |
---|
128 | (stream-take 10 primes) |
---|
129 | </expr> |
---|
130 | <result>(2 3 5 7 11 13 17 19 23 29)</result> |
---|
131 | <expr>(stream-ref primes 200)</expr> |
---|
132 | <result>1229</result> |
---|
133 | </example></examples> |
---|
134 | |
---|
135 | <examples><example> |
---|
136 | <expr> |
---|
137 | (define mul-streams |
---|
138 | (lambda (s1 s2) |
---|
139 | (stream-cons (* (stream-car s1) (stream-car s2)) |
---|
140 | (mul-streams (stream-cdr s1) (stream-cdr s2))))) |
---|
141 | (define integer-reciprocals |
---|
142 | (lambda () |
---|
143 | (let loop ((s (stream-cdr integers))) |
---|
144 | (stream-cons (/ 1 (stream-car s)) |
---|
145 | (loop (stream-cdr s)))))) |
---|
146 | (define integrate-series |
---|
147 | (lambda (s) |
---|
148 | (mul-streams s (integer-reciprocals)))) |
---|
149 | (define exp-series |
---|
150 | (stream-cons 1 (integrate-series exp-series))) |
---|
151 | (define partial-sums |
---|
152 | (lambda (s) |
---|
153 | (let loop ((s s) (a 0)) |
---|
154 | (stream-cons (+ a (stream-car s)) |
---|
155 | (loop (stream-cdr s) (+ a (stream-car s))))))) |
---|
156 | |
---|
157 | (stream-ref (partial-sums exp-series) 20) |
---|
158 | </expr> |
---|
159 | <result>2.71828182845905</result> |
---|
160 | </example></examples> |
---|
161 | |
---|
162 | === Changelog |
---|
163 | |
---|
164 | * 1.6 (Ivan Raikov) Added options to the compilation command to generate exports file. |
---|
165 | * 1.5 (Alejandro Forero Cuervo) Removed call to {{(register-feature! 'srfi-40)}}, which was confusing {{require-extension}} |
---|
166 | * 1.4 Fixed bug in {{stream-filter}} |
---|
167 | * 1.3 {{make-stream}} is hidden (but still exported) |
---|
168 | * 1.2 Some fixes to the basic stream handling were applied by Alejandro Forero Cuervo. |
---|
169 | * 1.1 Adapted to new setup scheme. |
---|
170 | * 1.0 Initial release |
---|
171 | |
---|
172 | === License |
---|
173 | |
---|
174 | Copyright (C) 2003 by Philip L. Bewig of Saint Louis, Missouri, United States of |
---|
175 | America. All rights reserved. |
---|