source: project/wiki/eggref/5/pseudolists @ 37449

Last change on this file since 37449 was 37449, checked in by juergen, 5 months ago

pseudolists docu

File size: 8.8 KB
Line 
1[[tags: egg]]
2[[toc:]]
3
4== Pseudolists
5
6This module exports routines to handle pseudolists as a generalisation
7of ordinary lists.
8
9=== Rationale
10
11Pseudolists differ from lists insofor, as their sentinels might be
12arbitrary atoms instead of the empty lists. In other words, a
13pseudolist is either a pair or an atom. But since an atom is simply not
14a pair, everything is a pseudolist, in particular, a list is one.
15
16So the routines of this module have to be written so that they work for
17lists as well. For example, this means, that the sentinel doesn't count
18in a length routine, and are not touched in a map routine. Moreover,
19constructors must supply the sentinel as an additional argument.
20
21Most functions in this module are written in curried and uncurried form.
22So the curried version can be used in maps and friends. The signature of
23both versions is documented below, but only the uncurried version is
24described.
25
26=== Module pseudolists
27
28==== pseudolists
29
30<procedure>(pseudolists)</procedure>
31<procedure>(pseudolists sym)</procedure>
32
33documentation procedure. The first call shows the list of exported
34routines, the second documentation of the routine with symbol sym.
35
36==== pl-maker
37
38<procedure>(pl-maker sentinel . args)</procedure>
39
40constructs a new pseudolist. Note the position of the sentineal
41argument.
42
43==== pl?
44
45<procedure>(pl? xpr)</procedure>
46
47type predicate. Note that everything is a pseudolist!
48
49==== pl-of?
50
51<procedure>(pl-of? . preds)</procedure>
52
53returns a unary predicate, which tests, if its argument is
54passed by each predicate in preds.
55
56==== pl-null?
57
58<procedure>(pl-null? xpr)</procedure>
59
60not a pair.
61
62==== pl-length
63
64<procedure>(pl-length pl)</procedure>
65
66length of a pseudolist. The sentinel is not counted.
67
68==== pl-iterate
69
70<procedure>(pl-iterate sentinel k fn)</procedure>
71<procedure>(pl-iterate sentinel k fn init)</procedure>
72
73creates a pseudolist with sentinel applying fn to init
74recursively k times.
75
76==== pl-at
77
78<procedure>(pl-at k)</procedure>
79<procedure>(pl-at k pl)</procedure>
80
81returns the kth item of pl.
82
83==== pl-head
84
85<procedure>(pl-head pl)</procedure>
86
87returns the list of items with pl's sentinel stripped.
88
89==== pl-sentinel
90
91<procedure>(pl-sentinel pl)</procedure>
92
93returns the sentinel of pl.
94
95==== pl-drop
96
97<procedure>(pl-drop pl)</procedure>
98<procedure>(pl-drop n pl)</procedure>
99
100returns the tail of pl removing all head items
101that pass the ok? test.
102
103==== pl-drop-while
104
105<procedure>(pl-drop-while pl)</procedure>
106<procedure>(pl-drop-while n pl)</procedure>
107
108returns the tail of pl starting with the first item
109that does not pass the ok? test.
110
111==== pl-take
112
113<procedure>(pl-take n pl)</procedure>
114<procedure>(pl-take pl)</procedure>
115
116returns the head of pl up to but excluding index n,
117where n is less than or equal to pl's pl-length.
118
119==== pl-take-while
120
121<procedure>(pl-take-while pl)</procedure>
122<procedure>(pl-take-while ok? pl)</procedure>
123
124returns the head of pl consisting of items
125which pass the ok? test.
126
127==== pl-map
128
129<procedure>(pl-map fn)</procedure>
130<procedure>(pl-map fn pl)</procedure>
131
132maps fn over the pseudolist pl.
133
134==== pl-index
135
136<procedure>(pl-index ok?)</procedure>
137<procedure>(pl-index ok? pl)</procedure>
138
139returns the index of the first item passing
140the ok? test, -1 otherwise.
141
142==== filter
143
144<procedure>(filter ok?)</procedure>
145<procedure>(filter ok? pl)</procedure>
146
147returns the sublist of lst consisting of all items passing the ok?
148predicate.
149
150==== pl-reverse
151
152<procedure>(pl-reverse pl)</procedure>
153
154reverses its pseudolist argument.
155
156==== pl-append
157
158<procedure>(pl-append pl . pls)</procedure>
159
160appends all argument pseudolists.
161
162==== pl-memp
163
164<procedure>(pl-memp ok?)</procedure>
165<procedure>(pl-memp ok? pl)</procedure>
166
167returns the sub-pseudolist starting at the first
168item which passes the ok? test.
169
170==== pl-member
171
172<procedure>(pl-member x)</procedure>
173<procedure>(pl-member x pl)</procedure>
174
175same as (pl-memp (cut equal? <> x) pl).
176
177==== pl-memq
178
179<procedure>(pl-memq x)</procedure>
180<procedure>(pl-memq x pl)</procedure>
181
182same as (pl-memp (cut eq? <> x) pl).
183
184==== pl-memv
185
186<procedure>(pl-memv x)</procedure>
187<procedure>(pl-memv x pl)</procedure>
188
189same as (pl-memp (cut eqv? <> x) pl).
190
191==== pl-fold-right
192
193<procedure>(pl-fold-right op init)</procedure>
194<procedure>(pl-fold-right op init pl)</procedure>
195
196folds pl from the right with binary operation op
197and starting value init.
198
199==== pl-fold-right0
200
201<procedure>(pl-fold-right0 op)</procedure>
202<procedure>(pl-fold-right0 op pl)</procedure>
203
204folds (cdr pl) from the right with binary operation op
205and starting value (car pl).
206
207==== pl-fold-left
208
209<procedure>(pl-fold-left op init)</procedure>
210<procedure>(pl-fold-left op init pl)</procedure>
211
212folds pl from the left with binary operation op
213and starting value init.
214
215==== pl-fold-left0
216
217<procedure>(pl-fold-left0 op)</procedure>
218<procedure>(pl-fold-left0 op pl)</procedure>
219
220folds (cdr pl) from the left with binary operation op
221and starting value (car pl).
222
223==== pl-adjoin
224
225<procedure>(adjoin obj)</procedure>
226<procedure>(adjoin obj pl)</procedure>
227
228adds obj to the pseudolist pl, provided obj is not an item of lst.
229
230==== pl-remove-dups
231
232<procedure>(pl-remove-dups pl)</procedure>
233
234removes all duplicates of lst.
235
236==== pl-flatten
237
238<procedure>(pl-flatten tree)</procedure>
239
240transforms a nested pseudolist to a flat list including sentinels.
241
242==== pl-for
243
244<macro>(pl-for ((var pl ok-xpr ...) (var1 pl1 ok-xpr1 ...) ...) item-xpr)</macro>
245
246creates a new pseudolist by binding var to each element
247of the pseudolist pl in sequence, and if it passes the checks,
248ok-xpr ..., inserts the value of item-xpr into the resulting pseudolist.
249The qualifieres, (var pl ok-xpr ...), are processed
250sequentially from left to right, so that filters of a
251qualifier have access to the variables of qualifiers
252to its left.
253
254=== Dependencies
255
256none
257
258=== Examples
259
260<enscript highlight=scheme>
261
262(import pseudolists)
263
264((pl-of? symbol?) '(a b . c)) ;-> #t
265(pl-maker #f 0 1 2 3) ;-> '(0 1 2 3 . #f)
266(pl-maker '() 0 1 2 3) ;-> '(0 1 2 3)
267(pl-iterate #f 5 add1 0) ;-> '(0 1 2 3 4 . #f)
268
269(pl-length '(0 1 2 3 . 4)) ;-> 4
270(pl-head '(0 . 1)) ;-> '(0)
271(pl-at 2 '(0 1 2 3 . #f)) ;-> 2
272(condition-case (pl-at 0 1)
273  ((exn) #f)) ;-> #f
274
275(pl-index odd? '(0 2 4 . 1)) ;-> -1
276(pl-drop 0 1) ;-> 1
277(pl-drop 2 '(0 1 2 3 . #f)) ;-> '(2 3 . #f)
278(pl-take-while negative? '(1 3 2 4 . #f)) ;-> #f
279
280(pl-filter odd? '(0 1 2 3 . 4)) ;-> '(1 3 . 4)
281(pl-map add1 '(0 1 2 3 . 4)) ;-> '(1 2 3 4 . 4)
282(pl-map add1 #f) ;-> #f
283(pl-reverse '(0 1 2 3 . 4)) ;-> '(3 2 1 0 . 4)
284
285(pl-append '(0 1) '(2 3) #f) ;-> '(0 1 2 3 . #f)
286(pl-adjoin 2 '(0 1 2 3 . #f)) ;-> '(0 1 2 3 . #f)
287(pl-adjoin 1 '()) ;-> '(1)
288(pl-remove-dups '(0 1 2 3 2 . 2)) ;-> '(0 1 3 2 . 2)
289(pl-flatten '(0 (1 (2 . 3) 4 . #f))) ;-> '(0 1 2 3 4 #f)
290
291(pl-for ((x '(0 1 2 3 . #f))) (add1 x)) ; map
292  ;-> '(1 2 3 4 . #f)
293(pl-for ((x '(0 1 2 3))) (add1 x)) ; map
294  ;-> '(1 2 3 4)
295(pl-for ((x '(0 1 2 3 4 5) (odd? x))) x) ; filter
296  ;-> '(1 3 5)
297(pl-for ((n '(0 1 2 3 4 5) (positive? n) (even? n)))
298            (* 10 n))
299  ;-> '(20 40)
300(pl-for ((c '(A B C)) (k '(1 2 3 4)))
301  (list c k))
302  ;-> '((A 1) (A 2) (A 3) (A 4)
303  ;     (B 1) (B 2) (B 3) (B 4)
304  ;     (C 1) (C 2) (C 3) (C 4))
305(pl-for ((c '(A B C . #f)) (k '(1 2 3 4 . #t))) (list c k))
306  ;-> '((A 1) (A 2) (A 3) (A 4)
307  ;     (B 1) (B 2) (B 3) (B 4)
308  ;     (C 1) (C 2) (C 3) (C 4) . #f)
309
310</enscript>
311
312== Last update
313
314Mar 24, 2019
315
316== Author
317
318[[/users/juergen-lorenz|Juergen Lorenz]]
319
320== License
321
322 Copyright (c) 2019, Juergen Lorenz
323 All rights reserved.
324
325 Redistribution and use in source and binary forms, with or without
326 modification, are permitted provided that the following conditions are
327 met:
328 
329 Redistributions of source code must retain the above copyright
330 notice, this list of conditions and the following disclaimer.
331 
332 Redistributions in binary form must reproduce the above copyright
333 notice, this list of conditions and the following disclaimer in the
334 documentation and/or other materials provided with the distribution.
335 Neither the name of the author nor the names of its contributors may be
336 used to endorse or promote products derived from this software without
337 specific prior written permission.
338   
339 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
340 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
341 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
342 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
343 HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
344 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
345 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
346 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
347 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
348 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
349 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
350
351== Version History
352
353; 1.0 : initial import
354
355
356
Note: See TracBrowser for help on using the repository browser.