<HTML><BODY>I think, that in semantic of for/..., I mean (for/... ... (values ...)), it is impossible.<br><br>Possible API could be <br><br>(for/collect ([a list] [b sum])<br> ([i (in-list '(1 2 3 4)])<br> (cond <br> [(oddp? i) (a i) (b i)]<br> [(< (b) 10) (b 1)]))<br><br>Returns (values '(2 4) 8)<br><br>Inside body of (for/collect ([id option ...] ...) body)<br>(id val) = Add val to collection<br>(id) = return current collection value<br><br>For for/fold-like one may make<br>(define-collector fold [fld #:bind v #:inner v])<br><br>Then<br>(for/fold ([sum 0]) ([i (in-list '(1 2 3 4)]) ... (values (+ sum i)))<br><br>Become<br>(for/collect ([sum fold :initial 0]) ([i (in-list '(1 2 3 4)]) ... (sum (+ (sum) i)) ...)<br><br>But I cannot imagine, how to make this without closures and set!.<br><br>Суббота, 18 января 2014, 13:51 -05:00 от "J. Ian Johnson" <ianj@ccs.neu.edu>:<br>
<blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;">
<div id="">Suffice it to say, my package does not have that expressive power. I'd be happy to collaborate with you to design a good extension to my package so that this kind of accumulation is easier.<br>
-Ian<br>
----- Original Message -----<br>
From: "Roman Klochkov" <<a href="sentmsg?compose&To=kalimehtar@mail.ru">kalimehtar@mail.ru</a>><br>
To: "J. Ian Johnson" <<a href="sentmsg?compose&To=ianj@ccs.neu.edu">ianj@ccs.neu.edu</a>><br>
Cc: <a href="sentmsg?compose&To=users@racket%2dlang.org">users@racket-lang.org</a><br>
Sent: Saturday, January 18, 2014 1:13:38 PM GMT -05:00 US/Canada Eastern<br>
Subject: Re[2]: [racket] Style. for/fold for/list for/lists<br>
<br>
It is great generalization for for/fold <br>
<br>
But still missed something like this: <br>
<br>
(iter (for i in l) <br>
(if (oddp i) (collect i into a) (collect i into b)) <br>
(when (> i 10) <br>
(collect i into a)) <br>
(finally (return a b))) <br>
<br>
Or in your for/acc <br>
(for/acc ([:type append] [:type append]) <br>
([i (in-list l)]) <br>
(if (odd? i) (values (if (> i 10) (list i i) (list i)) null) <br>
(values (if (> i 10) (list i) null) (list i)))) <br>
<br>
I had to double condition and the result is not so transparent. <br>
<br>
Simple solution is: <br>
<br>
(define (make-collector init add post) <br>
(let ([a init]) <br>
(case-lambda <br>
[(val) (set! a (add val a)] <br>
[() a]))) <br>
<br>
(define (make-list-collector [init '()]) (make-collector init cons reverse)) <br>
<br>
(let ([a (make-list-collector)] <br>
[b (make-list-collector)]) <br>
(for ([i (in-list l)]) <br>
(if (odd? i) (a i) (b i)) <br>
(when (> i 10) (a i))) <br>
(values (a) (b))) <br>
<br>
But they say, that purity is the best and one should avoid set!... so this way is the dark way, isn't it? <br>
<br>
<br>
Суббота, 18 января 2014, 8:02 -05:00 от "J. Ian Johnson" <<a href="sentmsg?compose&To=ianj@ccs.neu.edu">ianj@ccs.neu.edu</a>>: <br>
<br>
<br>
<br>
<br>
<br>
<br>
You have hit limitations of the for/fold macros. I wrote an extension for controlling the way values get accumulated, along with post-processing like list reversal. Let me know what you think! <br>
<br>
<a href="https://github.com/ianj/nifty-macros/tree/master/for-acc" target="_blank">https://github.com/ianj/nifty-macros/tree/master/for-acc</a> <br>
-Ian <br>
----- Original Message ----- <br>
From: Roman Klochkov < <a href="sentmsg?compose&To=kalimehtar@mail.ru">kalimehtar@mail.ru</a> > <br>
To: <a href="sentmsg?compose&To=users@racket%2dlang.org">users@racket-lang.org</a> <br>
Sent: Sat, 18 Jan 2014 06:25:10 -0500 (EST) <br>
Subject: [racket] Style. for/fold for/list for/lists <br>
<br>
Where can I find style guide for these <br>
<br>
for/lists: <br>
<br>
I have to duplicate values names: <br>
<br>
(define-values ([(desc-args lengths add-data) <br>
(for/lists (desc-args lengths add-data) ([arg (in-list %args)]) <br>
....)) is it ok? Or better <br>
(define-values ([(desc-args lengths add-data) <br>
(for/lists (a b c) (...) ...)) <br>
<br>
? <br>
<br>
for/fold has no "return" clause. So I have to do <br>
(call-with-values <br>
(for/fold (acc1 acc2 sum) (...) ...) <br>
(lambda (acc1 acc2 sum) (reverse acc1) (reverse acc2) sum)) <br>
<br>
And for/list doesn't allow to add several items to resulting list in one round. <br>
Even with for/fold it gives <br>
<br>
(reverse <br>
(for/fold (acc) ([item (in-list list)]) <br>
(values (cons (get-val1 item) <br>
(cons (get-val2 item) acc))))) <br>
<br>
Awful, especially for reading. <br>
<br>
It is clumsy and unobvious. <br>
In Common Lisp iterate I could do <br>
(iter (...) <br>
(collect (foo) into acc1) <br>
(collect (bar) into acc2) <br>
(summing ... into sum) <br>
(finally (return acc1 acc2 sum))) <br>
<br>
<a href="http://common-lisp.net/project/iterate/doc/index.html" target="_blank">http://common-lisp.net/project/iterate/doc/index.html</a> <br>
<br>
Is there something similar? <br>
<br>
-- <br>
Roman Klochkov <br>
<br>
<br>
-- <br>
Roman Klochkov <br>
</div>
</blockquote>
<br>
<br>--
<br>Roman Klochkov<br></BODY></HTML>