nendo 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/emacs/nendo-mode.el +2 -0
- data/lib/init.nnd +51 -35
- data/lib/init.nndc +3371 -2920
- data/lib/nendo.rb +463 -194
- data/lib/srfi-2.nndc +48 -165
- data/lib/srfi-26.nndc +142 -511
- data/lib/text/html-lite.nndc +23 -1
- data/lib/util/combinations.nnd +290 -0
- data/lib/util/combinations.nndc +7218 -0
- data/lib/util/list.nndc +138 -387
- data/lib/util/match.nnd +672 -0
- data/lib/util/match.nndc +81024 -0
- data/test/match-test.nnd +186 -0
- data/test/nendo-util-test.nnd +5 -7
- data/test/nendo_spec.rb +697 -235
- data/test/syntax_spec.rb +561 -52
- data/test/util-combinations-test.nnd +383 -0
- metadata +9 -4
- data/example/scratch.nnd +0 -119
data/lib/text/html-lite.nndc
CHANGED
@@ -1367,7 +1367,29 @@ trampCall(
|
|
1367
1367
|
))
|
1368
1368
|
]
|
1369
1369
|
)) ,
|
1370
|
-
|
1370
|
+
trampCall( self._cons_METHOD( 'cons',
|
1371
|
+
begin
|
1372
|
+
if @global_lisp_binding.has_key?('_cons') then
|
1373
|
+
trampCall(@_cons)
|
1374
|
+
else raise NameError.new( "Error: undefined variable _cons", "_cons" ) end
|
1375
|
+
rescue => __e ; __e.set_backtrace( [":1"] + __e.backtrace ) ; raise __e
|
1376
|
+
end ,
|
1377
|
+
[
|
1378
|
+
LispKeyword.new( "empty?" ) ,
|
1379
|
+
trampCall( self._cons_METHOD( 'cons',
|
1380
|
+
begin
|
1381
|
+
if @global_lisp_binding.has_key?('_cons') then
|
1382
|
+
trampCall(@_cons)
|
1383
|
+
else raise NameError.new( "Error: undefined variable _cons", "_cons" ) end
|
1384
|
+
rescue => __e ; __e.set_backtrace( [":1"] + __e.backtrace ) ; raise __e
|
1385
|
+
end ,
|
1386
|
+
[
|
1387
|
+
true ,
|
1388
|
+
Cell.new()
|
1389
|
+
]
|
1390
|
+
))
|
1391
|
+
]
|
1392
|
+
))
|
1371
1393
|
]
|
1372
1394
|
))
|
1373
1395
|
]
|
@@ -0,0 +1,290 @@
|
|
1
|
+
;;;-*- mode: nendo; syntax: scheme -*-;;
|
2
|
+
;;;
|
3
|
+
;;; combinations.scm - combinations and that sort of stuff.
|
4
|
+
;;;
|
5
|
+
;;; Copyright(C) 2003 by Alex Shinn (foof@synthcode.com)
|
6
|
+
;;; Copyright (c) 2003-2010 Shiro Kawai <shiro@acm.org>
|
7
|
+
;;;
|
8
|
+
;;; Permission to use, copy, modify, distribute this software and
|
9
|
+
;;; accompanying documentation for any purpose is hereby granted,
|
10
|
+
;;; provided that existing copyright notices are retained in all
|
11
|
+
;;; copies and that this notice is included verbatim in all
|
12
|
+
;;; distributions.
|
13
|
+
;;; This software is provided as is, without express or implied
|
14
|
+
;;; warranty. In no circumstances the author(s) shall be liable
|
15
|
+
;;; for any damages arising out of the use of this software.
|
16
|
+
;;;
|
17
|
+
|
18
|
+
;; Initially written by Alex Shinn.
|
19
|
+
;; Modifided by Shiro Kawai
|
20
|
+
|
21
|
+
(use srfi-1)
|
22
|
+
(use srfi-26)
|
23
|
+
(use util.match)
|
24
|
+
|
25
|
+
;;----------------------------------------------------------------
|
26
|
+
;; permuations
|
27
|
+
;;
|
28
|
+
|
29
|
+
;; return a list of k-th element is removed
|
30
|
+
(define (but-kth lis k)
|
31
|
+
(case k
|
32
|
+
[(0) (cdr lis)]
|
33
|
+
[(1) (cons (car lis) (cddr lis))]
|
34
|
+
[(2) (list* (car lis) (cadr lis) (cdddr lis))]
|
35
|
+
[(3) (list* (car lis) (cadr lis) (caddr lis) (cddddr lis))]
|
36
|
+
[else (receive (head tail) (split-at lis k)
|
37
|
+
(append! head (cdr tail)))]))
|
38
|
+
|
39
|
+
;; permute set. all elements are considered distinct.
|
40
|
+
;; the shortcut for 3 elements or less speeds up a bit.
|
41
|
+
(define (permutations set)
|
42
|
+
(match set
|
43
|
+
[() (list '())]
|
44
|
+
[(a) (list set)]
|
45
|
+
[(a b) `(,set (,b ,a))]
|
46
|
+
[(a b c)
|
47
|
+
`(,set (,a ,c ,b) (,b ,a ,c) (,b ,c ,a) (,c ,a ,b) (,c ,b ,a))]
|
48
|
+
[else
|
49
|
+
(append-map
|
50
|
+
(lambda (ind head)
|
51
|
+
(map
|
52
|
+
(lambda (rest)
|
53
|
+
(cons head rest))
|
54
|
+
(permutations (but-kth set ind))))
|
55
|
+
(iota (length set))
|
56
|
+
set)]))
|
57
|
+
|
58
|
+
;; permute set, considering equal elements, a.k.a multiset permutations
|
59
|
+
(define (permutations* set :optional (eq eqv?))
|
60
|
+
(define (rec set)
|
61
|
+
(match set
|
62
|
+
[() (list '())]
|
63
|
+
[(a) (list set)]
|
64
|
+
[(a b) (if (eq a b) (list set) `(,set (,b ,a)))]
|
65
|
+
[else
|
66
|
+
(let loop ((i 0)
|
67
|
+
(seen '())
|
68
|
+
(p set)
|
69
|
+
(r '()))
|
70
|
+
(cond [(null? p) (reverse! r)]
|
71
|
+
[(member (car p) seen eq) (loop (+ i 1) seen (cdr p) r)]
|
72
|
+
[else
|
73
|
+
(loop (+ i 1)
|
74
|
+
(cons (car p) seen)
|
75
|
+
(cdr p)
|
76
|
+
(fold (lambda (subperm r) (acons (car p) subperm r))
|
77
|
+
r
|
78
|
+
(rec (but-kth set i))))]))]))
|
79
|
+
(rec set))
|
80
|
+
|
81
|
+
;; permutations without generating entire list.
|
82
|
+
;; We use shortcut for (<= length 4) case, which boosts performace.
|
83
|
+
(define (p/each3 proc x1 x2 x3)
|
84
|
+
(proc `(,x1 ,x2 ,x3)) (proc `(,x1 ,x3 ,x2))
|
85
|
+
(proc `(,x2 ,x1 ,x3)) (proc `(,x2 ,x3 ,x1))
|
86
|
+
(proc `(,x3 ,x1 ,x2)) (proc `(,x3 ,x2 ,x1)))
|
87
|
+
(define (p/each4 proc x1 x2 x3 x4)
|
88
|
+
(p/each3 (lambda (xs) (proc (cons x1 xs))) x2 x3 x4)
|
89
|
+
(p/each3 (lambda (xs) (proc (cons x2 xs))) x1 x3 x4)
|
90
|
+
(p/each3 (lambda (xs) (proc (cons x3 xs))) x1 x2 x4)
|
91
|
+
(p/each3 (lambda (xs) (proc (cons x4 xs))) x1 x2 x3))
|
92
|
+
(define (p/each* proc len xs)
|
93
|
+
(if (= len 4)
|
94
|
+
(apply p/each4 proc xs)
|
95
|
+
(let1 len1 (- len 1)
|
96
|
+
(for-each
|
97
|
+
(lambda (ind elt)
|
98
|
+
(p/each* (lambda (subperm) (proc (cons elt subperm)))
|
99
|
+
len1
|
100
|
+
(but-kth xs ind)))
|
101
|
+
(iota (length xs))
|
102
|
+
xs))))
|
103
|
+
(define (permutations-for-each proc set)
|
104
|
+
(match set
|
105
|
+
[() nil]
|
106
|
+
[(x) (proc set)]
|
107
|
+
[(x1 x2) (proc `(,x1 ,x2)) (proc `(,x2 ,x1))]
|
108
|
+
[(x1 x2 x3) (p/each3 proc x1 x2 x3)]
|
109
|
+
[(x1 x2 x3 x4) (p/each4 proc x1 x2 x3 x4)]
|
110
|
+
[else (p/each* proc (length set) set)]))
|
111
|
+
|
112
|
+
;; Like permutations-for-each, but considering duplications.
|
113
|
+
(define (permutations*-for-each proc set :optional (eq eqv?))
|
114
|
+
(define (rec proc set)
|
115
|
+
(match set
|
116
|
+
[() nil]
|
117
|
+
[(a) (proc set)]
|
118
|
+
[(a b) (cond [(eq a b) (proc set)] [else (proc set) (proc `(,b ,a))])]
|
119
|
+
[else
|
120
|
+
(let loop ((i 0)
|
121
|
+
(seen '())
|
122
|
+
(p set))
|
123
|
+
(cond [(null? p)]
|
124
|
+
[(member (car p) seen eq) (loop (+ i 1) seen (cdr p))]
|
125
|
+
[else (rec (lambda (subperm) (proc (cons (car p) subperm)))
|
126
|
+
(but-kth set i))
|
127
|
+
(loop (+ i 1) (cons (car p) seen) (cdr p))]))]))
|
128
|
+
(rec proc set))
|
129
|
+
|
130
|
+
;;----------------------------------------------------------------
|
131
|
+
;; combinations
|
132
|
+
;;
|
133
|
+
|
134
|
+
(define (combinations set n)
|
135
|
+
(if (not (positive? n))
|
136
|
+
(list '())
|
137
|
+
(pair-fold-right
|
138
|
+
(lambda (pr acc)
|
139
|
+
(fold-right (cut acons (car pr) <> <>)
|
140
|
+
acc
|
141
|
+
(combinations (cdr pr) (- n 1))))
|
142
|
+
'()
|
143
|
+
set)))
|
144
|
+
|
145
|
+
(define (combinations* set n :optional (eq eqv?))
|
146
|
+
(define (rec set n)
|
147
|
+
(if (not (positive? n))
|
148
|
+
(list '())
|
149
|
+
(let loop ((p set)
|
150
|
+
(seen '())
|
151
|
+
(r '()))
|
152
|
+
(cond [(null? p) (reverse! r)]
|
153
|
+
[(member (car p) seen eq) (loop (cdr p) seen r)]
|
154
|
+
[else
|
155
|
+
(loop (cdr p)
|
156
|
+
(cons (car p) seen)
|
157
|
+
(fold (cut acons (car p) <> <>)
|
158
|
+
r
|
159
|
+
(rec (lset-difference eq (cdr p) seen) (- n 1))))]
|
160
|
+
))))
|
161
|
+
(rec set n))
|
162
|
+
|
163
|
+
(define (combinations-for-each proc set n)
|
164
|
+
(if (not (positive? n))
|
165
|
+
(proc '())
|
166
|
+
(pair-for-each
|
167
|
+
(lambda (pr)
|
168
|
+
(combinations-for-each
|
169
|
+
(lambda (sub-comb) (proc (cons (car pr) sub-comb)))
|
170
|
+
(cdr pr)
|
171
|
+
(- n 1)))
|
172
|
+
set)))
|
173
|
+
|
174
|
+
(define (combinations*-for-each proc set n :optional (eq eqv?))
|
175
|
+
(define (rec proc set n)
|
176
|
+
(if (not (positive? n))
|
177
|
+
(proc '())
|
178
|
+
(let loop ((p set)
|
179
|
+
(seen '()))
|
180
|
+
(cond [(null? p)]
|
181
|
+
[(member (car p) seen eq) (loop (cdr p) seen)]
|
182
|
+
[else
|
183
|
+
(rec (lambda (sub-comb) (proc (cons (car p) sub-comb)))
|
184
|
+
(lset-difference eq (cdr p) seen)
|
185
|
+
(- n 1))
|
186
|
+
(loop (cdr p) (cons (car p) seen))]))))
|
187
|
+
(rec proc set n))
|
188
|
+
|
189
|
+
;;----------------------------------------------------------------
|
190
|
+
;; power sets (all subsets of any size of a given set)
|
191
|
+
;;
|
192
|
+
|
193
|
+
;; the easy binary way
|
194
|
+
(define (power-set-binary set)
|
195
|
+
(if (null? set)
|
196
|
+
(list '())
|
197
|
+
(let ((x (car set))
|
198
|
+
(rest (power-set-binary (cdr set))))
|
199
|
+
(append rest (map (lambda (s) (cons x s)) rest)))))
|
200
|
+
|
201
|
+
;; use combinations for nice ordering
|
202
|
+
(define (power-set set)
|
203
|
+
(let ((size (length set)))
|
204
|
+
(let loop ((i 0))
|
205
|
+
(if (> i size)
|
206
|
+
'()
|
207
|
+
(append! (combinations set i)
|
208
|
+
(loop (+ i 1)))))))
|
209
|
+
|
210
|
+
;; also ordered
|
211
|
+
(define (power-set-for-each proc set)
|
212
|
+
(let ((size (length set)))
|
213
|
+
(let loop ((i 0))
|
214
|
+
(if (> i size)
|
215
|
+
'()
|
216
|
+
(begin
|
217
|
+
(combinations-for-each proc set i)
|
218
|
+
(loop (+ i 1)))))))
|
219
|
+
|
220
|
+
;; w/o duplicate entry
|
221
|
+
(define (power-set* set . maybe-eq)
|
222
|
+
(let ((size (length set)))
|
223
|
+
(let loop ((i 0))
|
224
|
+
(if (> i size)
|
225
|
+
'()
|
226
|
+
(append! (apply combinations* set i maybe-eq)
|
227
|
+
(loop (+ i 1)))))))
|
228
|
+
|
229
|
+
(define (power-set*-for-each proc set . maybe-eq)
|
230
|
+
(let ((size (length set)))
|
231
|
+
(let loop ((i 0))
|
232
|
+
(if (> i size)
|
233
|
+
'()
|
234
|
+
(begin
|
235
|
+
(apply combinations*-for-each proc set i maybe-eq)
|
236
|
+
(loop (+ i 1)))))))
|
237
|
+
|
238
|
+
;;----------------------------------------------------------------
|
239
|
+
;; cartesian product (all combinations of one element from each set)
|
240
|
+
;;
|
241
|
+
|
242
|
+
(define (cartesian-product lol)
|
243
|
+
(if (null? lol)
|
244
|
+
(list '())
|
245
|
+
(let ((l (car lol))
|
246
|
+
(rest (cartesian-product (cdr lol))))
|
247
|
+
(append-map!
|
248
|
+
(lambda (x)
|
249
|
+
(map (lambda (sub-prod) (cons x sub-prod)) rest))
|
250
|
+
l))))
|
251
|
+
|
252
|
+
(define (cartesian-product-for-each proc lol)
|
253
|
+
(if (null? lol)
|
254
|
+
(proc '())
|
255
|
+
(for-each
|
256
|
+
(lambda (x)
|
257
|
+
(cartesian-product-for-each
|
258
|
+
(lambda (sub-prod)
|
259
|
+
(proc (cons x sub-prod)))
|
260
|
+
(cdr lol)))
|
261
|
+
(car lol))))
|
262
|
+
|
263
|
+
;; The above is left fixed (it varies elements to the right first).
|
264
|
+
;; Below is a right fixed product which could be defined with two
|
265
|
+
;; reverses but is short enough to warrant the performance gain of a
|
266
|
+
;; separate procedure.
|
267
|
+
|
268
|
+
;;(define (cartesian-product-right lol)
|
269
|
+
;; (map reverse (cartesian-product (reverse lol))))
|
270
|
+
|
271
|
+
(define (cartesian-product-right lol)
|
272
|
+
(if (null? lol)
|
273
|
+
(list '())
|
274
|
+
(let ((l (car lol))
|
275
|
+
(rest (cartesian-product (cdr lol))))
|
276
|
+
(append-map!
|
277
|
+
(lambda (sub-prod)
|
278
|
+
(map (lambda (x) (cons x sub-prod)) l))
|
279
|
+
rest))))
|
280
|
+
|
281
|
+
(define (cartesian-product-right-for-each proc lol)
|
282
|
+
(if (null? lol)
|
283
|
+
(proc '())
|
284
|
+
(cartesian-product-right-for-each
|
285
|
+
(lambda (sub-prod)
|
286
|
+
(for-each (lambda (x) (proc (cons x sub-prod))) (car lol)))
|
287
|
+
(cdr lol))))
|
288
|
+
|
289
|
+
|
290
|
+
|