source: project/wiki/eggref/5/list-comprehensions @ 37636

Last change on this file since 37636 was 37517, checked in by svnwiki, 18 months ago

Anonymous wiki edit for IP [139.174.198.234]: small markup fix: </macro> to <macro>

File size: 4.7 KB
Line 
1[[tags: egg]]
2[[toc:]]
3
4
5== List comprehensions
6
7List comprehensions are popularized by other functional languages like
8Standard ML, Miranda, Haskell.
9
10The implementation of this module is inspired by Clojure, in particular
11the for macro.
12
13Other list-generating procedures, like range, repeat, iterate-times,
14iterate-until and iterate-while are provided as well.
15
16=== The API
17
18==== list-comprehensions
19
20<procedure>(list-comprehensions sym ..)</procedure>
21
22Documentation procedure. Without argument, returns the list of exported
23symbols, with argument the documentation of that symbol.
24
25==== range
26
27<procedure>(range upto)</procedure>
28<procedure>(range from upto)</procedure>
29<procedure>(range from upto step)</procedure>
30
31creates a list of numbers with given limits
32from defaults to 0
33step defaults to 1
34
35==== repeat
36
37<procedure>(repeat times)</procedure>
38
39returns a unary procedure which repeats its only argument
40a number of times
41
42==== iterate-times
43
44<procedure>(iterate-times fn times)</procedure>
45
46returns a unary procedure which iterates the function fn
47on its only argument a number of times
48
49==== iterate-while
50
51<procedure>(iterate-while fn ok?)</procedure>
52
53returns a unary procedure which iterates the function fn
54on its only argument while the predicate ok? returns true
55
56==== iterate-until
57
58<procedure>(iterate-until fn ok?)</procedure>
59
60returns a unary procedure which iterates the function fn
61on its only argument until the predicate ok? returns true
62
63==== for
64
65<macro>(for (qualifier ....) item)</macro>
66
67where each qualifier is of the form (var lst fltr ...) whith
68
69* var a variable
70* lst a list from which values are chosen
71* fltr a filter expression
72
73Creates a new list by binding var to each element
74of lst in sequence, and if it passes the checks fltr ...,
75inserts the item expression into the result list.
76The qualifieres are processed sequentially from left to right,
77so that filters of a qualifier have access to the variables of
78qualifiers to its left.
79
80For example
81
82<enscript highlight=scheme>
83  (for ((x (list #\A #\B))
84        (y (list 1 2))
85        (z (list #f #t)))
86       (list x y z))
87</enscript>
88
89produces
90
91<enscript highlight=scheme>
92  ((#\A 1 #f)
93   (#\A 1 #t)
94   (#\A 2 #f)
95   (#\A 2 #t)
96   (#\B 1 #f)
97   (#\B 1 #t)
98   (#\B 2 #f)
99   (#\B 2 #t))
100</enscript>
101
102=== Requirements
103
104None
105
106=== Examples
107
108<enscript highlight=scheme>
109
110(import list-comprehensions)
111
112(range 0) ; -> '()
113
114(range 5) ; -> '(0 1 2 3 4)
115
116(range -5) ; -> '(0 -1 -2 -3 -4)
117
118(range 1 5) ; -> '(1 2 3 4)
119
120(range 5 1) ; -> '(5 4 3 2)
121
122(range 1 5 2) ; -> '(1 3)
123
124((repeat 5) 'x) ; -> '(x x x x x)
125
126((iterate-times add1 5) 1) ; -> '(1 2 3 4 5)
127
128((iterate-until sub1 zero?) 5) ; -> '(5 4 3 2 1)
129
130((iterate-while sub1 positive?) 5) ; -> '(5 4 3 2 1)
131
132(for ((c (string->list "ABCDEFG"))
133      (k '(1 2 3 4 5 6 7 8)))
134     (list c k))
135; -> ((#\A 1) ... (#\A 8) ... (#\G 1) ... (#\G 8))
136
137(for ((c (string->list "ABCDEFG"))
138      (k '(1 2 3 4 5 6 7 8) (memv c '(#\A #\G)) (memv k '(1 8))))
139     (list c k))
140; -> ((#\A 1) (#\A 8) (#\G 1) (#\G 8))
141
142(for ((x '(0 1 2 3))) (add1 x)) ; map
143; -> '(1 2 3 4)
144
145(for ((x '(0 1 2 3 4 5) (odd? x))) x) ; filter
146; -> '(1 3 5)
147</enscript>
148
149== Last update
150
151Jan 28, 2019
152
153== Author
154
155[[/users/juergen-lorenz|Juergen Lorenz]]
156
157== License
158
159 Copyright (c) 2017-2019, Juergen Lorenz
160 All rights reserved.
161
162 Redistribution and use in source and binary forms, with or without
163 modification, are permitted provided that the following conditions are
164 met:
165 
166 Redistributions of source code must retain the above copyright
167 notice, this list of conditions and the following disclaimer.
168 
169 Redistributions in binary form must reproduce the above copyright
170 notice, this list of conditions and the following disclaimer in the
171 documentation and/or other materials provided with the distribution.
172 Neither the name of the author nor the names of its contributors may be
173 used to endorse or promote products derived from this software without
174 specific prior written permission.
175   
176 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
177 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
178 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
179 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
180 HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
181 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
182 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
183 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
184 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
185 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
186 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
187
188== Version History
189; 1.0 : initial import for chicken-5
190
Note: See TracBrowser for help on using the repository browser.