[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Python syntax in Lisp and Scheme

William James

7/29/2015 9:17:00 AM

Pascal Costanza wrote:

> 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#...

Gauche Scheme:

(define (predicate-collect items . predicates)
(let go ((xs items) (predicates predicates))
(if (null? predicates)
(list xs)
(receive (picked rejected) (partition (car predicates) xs)
(cons picked (go rejected (cdr predicates)))))))

(predicate-collect '(-5 -4 -3 -2 -1 0 1 2 3 4 5)
even?
negative?
(cut > <> 3))

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

--
You have politicians saying that ... as many Africans as want to come into
Sweden should be able to come.... They've already said that everybody from
Syria can come to Sweden.... [T]hey are actually thinking of commandeering
people's vacation homes because they need more housing for immigrants.
--- Dr. Kevin MacDonald (http://lnrlive.com/tpc/tpc201...)
1 Answer

William James

3/30/2016 12:06:00 AM

0

WJ wrote:

> Pascal Costanza wrote:
>
> > 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#...

OCaml:

let multi_partition items predicates =
let rec loop items = function
[] -> [items]
| p::preds -> let (yes,no) = List.partition p items
in yes :: loop no preds
in loop items predicates ;;

multi_partition [-5;-4;-3;-2;-1;0;1;2;3;4;5]
[(fun n -> 0=n land 1);
((>) 0);
((<) 3)] ;;

===>
[[-4; -2; 0; 2; 4]; [-5; -3; -1]; [5]; [1; 3]]

--
"To destroy a nation, convince its members that nationalism is evil.
To destroy a race, convince its members that racialism is evil."