Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 43 |
Nodes: | 6 (0 / 6) |
Uptime: | 98:27:06 |
Calls: | 290 |
Files: | 905 |
Messages: | 76,483 |
What about dealing with an arbitrary number of filters?
(defmacro predicate-collect (list &body predicates)
(let ((collectors (mapcar (lambda (predicate)
(declare (ignore predicate))
(gensym "COLLECT"))
predicates))
(collect-t (gensym "COLLECT")))
`(with-collectors (,@collectors ,collect-t)
(dolist (l ,list)
(cond ,@(mapcar (lambda (predicate collector)
`((funcall ,predicate l) (,collector l)))
predicates collectors)
(t (,collect-t l)))))))
An example:
(predicate-collect '(-5 -4 -3 -2 -1 0 1 2 3 4 5)(function evenp)
(lambda (n) (< n 0))
(lambda (n) (> n 3)))
(-4 -2 0 2 4)
(-5 -3 -1)
(5)
(1 3)
I use the list collector macros by Tim Bradshaw here - see http://www.tfeb.org/lisp/hax.html#COLLECTING
Scheme
(define vector-fill!
(lambda (v x)
(let ((n (vector-length v)))
(do ((i 0 (+ i 1)))
((= i n))
(vector-set! v i x)))))
I think you could write the scheme code like this:
(define vector-fill! (v x)
(let ((i 0))
(while (< i (length v))
(vector-set! v i x)
(set! i (1+ i)))))