Discussion:
Python syntax in Lisp and Scheme
(too old to reply)
B. Pym
2024-09-13 19:22:53 UTC
Permalink
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")))
(dolist (l ,list)
`((funcall ,predicate l) (,collector l)))
predicates collectors)
(t (,collect-t l)))))))
(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
Gauche Scheme

(define (multi-partition the-list . predicates)
(if (null? predicates)
(list the-list)
(receive (yes no)
(partition (car predicates) the-list)
(cons yes (apply multi-partition no (cdr predicates))))))

(multi-partition '(-5 -4 -3 -2 -1 0 1 2 3 4 5)
even?
(is < 0)
(is > 3))

((-4 -2 0 2 4) (-5 -3 -1) (5) (1 3))

Given:

(define-syntax is
(syntax-rules ()
[(is x)
(lambda (y) (equal? y x))]
[(is compare x)
(lambda (y) (compare y x))]
[(is key compare x)
(lambda (y) (compare (key y) x))]))
B. Pym
2024-09-13 20:38:02 UTC
Permalink
Post by B. Pym
Scheme
(define vector-fill!
(lambda (v x)
(let ((n (vector-length v)))
(do ((i 0 (+ i 1)))
((= i n))
(vector-set! v i x)))))
(define vector-fill! (v x)
(let ((i 0))
(while (< i (length v))
(vector-set! v i x)
(set! i (1+ i)))))
Gauche Scheme

(define myvec (vector 0 0 0 0 0))
(vector-fill! myvec 99)

Loading...