1 | ; Author: Juergen Lorenz ; ju (at) jugilo (dot) de |
---|
2 | ; |
---|
3 | ; Copyright (c) 2017-2019, Juergen Lorenz |
---|
4 | ; All rights reserved. |
---|
5 | ; |
---|
6 | ; Redistribution and use in source and binary forms, with or without |
---|
7 | ; modification, are permitted provided that the following conditions are |
---|
8 | ; met: |
---|
9 | ; |
---|
10 | ; Redistributions of source code must retain the above copyright |
---|
11 | ; notice, this list of conditions and the following dispasser. |
---|
12 | ; |
---|
13 | ; Redistributions in binary form must reproduce the above copyright |
---|
14 | ; notice, this list of conditions and the following dispasser in the |
---|
15 | ; documentation and/or other materials provided with the distribution. |
---|
16 | ; |
---|
17 | ; Neither the name of the author nor the names of its contributors may be |
---|
18 | ; used to endorse or promote products derived from this software without |
---|
19 | ; specific prior written permission. |
---|
20 | ; |
---|
21 | ; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
---|
22 | ; IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
---|
23 | ; TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
---|
24 | ; PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
25 | ; HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
26 | ; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
---|
27 | ; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
28 | ; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
29 | ; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
30 | ; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
31 | ; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | |
---|
33 | |
---|
34 | #|[ |
---|
35 | This module implements some list creating routines, in particular a |
---|
36 | version of Clojure's equally named macro for, which is to replace |
---|
37 | list-comprehension syntax, e.g. [n^2 | n<-[1..10]; n mod 2 = 0] |
---|
38 | of purely functional languages like Miranda. |
---|
39 | ]|# |
---|
40 | |
---|
41 | (module list-comprehensions |
---|
42 | (list-comprehensions for range repeat |
---|
43 | iterate-while iterate-until iterate-times) |
---|
44 | |
---|
45 | (import scheme |
---|
46 | (only (chicken base) case-lambda error print)) |
---|
47 | |
---|
48 | (define range |
---|
49 | (case-lambda |
---|
50 | ((upto) |
---|
51 | (if (>= upto 0) |
---|
52 | (range 0 upto 1) |
---|
53 | (range 0 upto -1))) |
---|
54 | ((from upto) |
---|
55 | (if (>= upto from) |
---|
56 | (range from upto 1) |
---|
57 | (range from upto -1))) |
---|
58 | ((from upto step) |
---|
59 | (cond |
---|
60 | ((and (>= upto from) (positive? step)) |
---|
61 | (let loop ((k from) (result '())) |
---|
62 | (if (>= k upto) |
---|
63 | (reverse result) |
---|
64 | (loop (+ k step) (cons k result))))) |
---|
65 | ((and (< upto from) (negative? step)) |
---|
66 | (let loop ((k from) (result '())) |
---|
67 | (if (<= k upto) |
---|
68 | (reverse result) |
---|
69 | (loop (+ k step) (cons k result))))) |
---|
70 | (else |
---|
71 | (error 'range "wrong sign of" step)))))) |
---|
72 | |
---|
73 | (define (repeat times) |
---|
74 | (lambda (x) |
---|
75 | (let loop ((k 0) (result '())) |
---|
76 | (if (= k times) |
---|
77 | result |
---|
78 | (loop (+ k 1) (cons x result)))))) |
---|
79 | |
---|
80 | (define (iterate-while fn ok?) |
---|
81 | (lambda (start) |
---|
82 | (let loop ((var start) (result '())) |
---|
83 | (if (ok? var) |
---|
84 | (loop (fn var) (cons var result)) |
---|
85 | (reverse result))))) |
---|
86 | |
---|
87 | (define (iterate-until fn ok?) |
---|
88 | (lambda (start) |
---|
89 | (let loop ((var start) (result '())) |
---|
90 | (if (ok? var) |
---|
91 | (reverse result) |
---|
92 | (loop (fn var) (cons var result)))))) |
---|
93 | |
---|
94 | (define (iterate-times fn times) |
---|
95 | (lambda (start) |
---|
96 | (let loop ((var start) (k 0) (result '())) |
---|
97 | (if (= k times) |
---|
98 | (reverse result) |
---|
99 | (loop (fn var) (+ k 1) (cons var result)))))) |
---|
100 | |
---|
101 | ;;; (for ((var lst fltr ...) ....) item) |
---|
102 | ;;; ------------------------------------ |
---|
103 | (define-syntax for |
---|
104 | (syntax-rules () |
---|
105 | ((_ ((var lst fltr ...)) item) |
---|
106 | (let recur ((seq lst)) |
---|
107 | (if (null? seq) |
---|
108 | '() |
---|
109 | (let ((var (car seq))) |
---|
110 | (if (and fltr ...) |
---|
111 | (cons item (recur (cdr seq))) |
---|
112 | (recur (cdr seq))))))) |
---|
113 | ((_ ((var lst fltr ...) (var1 lst1 fltr1 ...) ...) item) |
---|
114 | (let recur ((seq lst)) |
---|
115 | (if (null? seq) |
---|
116 | '() |
---|
117 | (let ((var (car seq))) |
---|
118 | (if (and fltr ...) |
---|
119 | (append (for ((var1 lst1 fltr1 ...) ...) item) |
---|
120 | (recur (cdr seq))) |
---|
121 | (recur (cdr seq))))))) |
---|
122 | )) |
---|
123 | |
---|
124 | |
---|
125 | ;;; (list-comprehensions sym ..) |
---|
126 | ;;; ---------------------------- |
---|
127 | ;;; documentation procedure |
---|
128 | (define list-comprehensions |
---|
129 | (let ((als '( |
---|
130 | (list-comprehensions |
---|
131 | procedure: |
---|
132 | (list-comprehensions sym ..) |
---|
133 | "documentation procedure") |
---|
134 | (range |
---|
135 | procedure: |
---|
136 | (range upto) |
---|
137 | (range from upto) |
---|
138 | (range from upto step) |
---|
139 | "creates a list of numbers with given limits" |
---|
140 | "from defaults to 0" |
---|
141 | "step defaults to 1") |
---|
142 | (repeat |
---|
143 | procedure: |
---|
144 | (repeat times) |
---|
145 | "returns a unary procedure which repeats its only argument" |
---|
146 | "a number of times") |
---|
147 | (iterate-times |
---|
148 | procedure: |
---|
149 | (iterate-times fn times) |
---|
150 | "returns a unary procedure which iterates the function fn" |
---|
151 | "on its only argument a number of times") |
---|
152 | (iterate-while |
---|
153 | procedure: |
---|
154 | (iterate-while fn ok?) |
---|
155 | "returns a unary procedure which iterates the function fn" |
---|
156 | "on its only argument while the predicate ok? returns true") |
---|
157 | (iterate-until |
---|
158 | procedure: |
---|
159 | (iterate-until fn ok?) |
---|
160 | "returns a unary procedure which iterates the function fn" |
---|
161 | "on its only argument until the predicate ok? returns true") |
---|
162 | (for |
---|
163 | macro: |
---|
164 | (for ((var lst fltr ...) ....) item) |
---|
165 | "creates a new list by binding var to each element" |
---|
166 | "of the list lst in sequence, and if it passes the checks," |
---|
167 | "fltr ..., inserts item into the result list." |
---|
168 | "The qualifieres, (var lst fltr ...), are processed" |
---|
169 | "sequentially from left to right, so that filters of a" |
---|
170 | "qualifier have access to the variables of qualifiers" |
---|
171 | "to its left.") |
---|
172 | ))) |
---|
173 | (case-lambda |
---|
174 | (() |
---|
175 | (map car als)) |
---|
176 | ((sym) |
---|
177 | (let ((pair (assq sym als))) |
---|
178 | (if pair |
---|
179 | (for-each print (cdr pair)) |
---|
180 | (error "Not in list" |
---|
181 | sym |
---|
182 | (map car als)))))))) |
---|
183 | |
---|
184 | ) ; module list-comprehensions |
---|
185 | |
---|