[racket] Style. for/fold for/list for/lists

From: Roman Klochkov (kalimehtar at mail.ru)
Date: Sat Jan 18 13:13:38 EST 2014

 It is great generalization for for/fold

But still missed something like this:

(iter (for i in l)
        (if (oddp i) (collect i into a) (collect i into b))
        (when (> i 10)
             (collect i into a))
        (finally (return a b)))

Or in your for/acc
(for/acc ([:type append] [:type append])
              ([i (in-list l)])
     (if (odd? i) (values (if (> i 10) (list i i) (list i)) null)
                        (values (if (> i 10) (list i) null) (list i))))

I had to double condition and the result is not so transparent.

Simple solution is:

(define (make-collector init add post)
   (let ([a init])
       (case-lambda 
           [(val) (set! a (add val a)]
           [() a])))

(define (make-list-collector [init '()]) (make-collector init cons reverse))

(let ([a (make-list-collector)]
        [b (make-list-collector)])
   (for ([i (in-list l)])
        (if (odd? i) (a i) (b i))
        (when (> i 10) (a i)))
   (values (a) (b)))

But they say, that purity is the best and one should avoid set!... so this way is the dark way, isn't it?


Суббота, 18 января 2014, 8:02 -05:00 от "J. Ian Johnson" <ianj at ccs.neu.edu>:
>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!
>
>https://github.com/ianj/nifty-macros/tree/master/for-acc
>-Ian
>----- Original Message -----
>From: Roman Klochkov < kalimehtar at mail.ru >
>To:  users at racket-lang.org
>Sent: Sat, 18 Jan 2014 06:25:10 -0500 (EST)
>Subject: [racket] Style. for/fold for/list for/lists
>
> Where can I find style guide for these
>
>for/lists:
>
>I have to duplicate values names:
>
>(define-values ([(desc-args lengths add-data)
>              (for/lists (desc-args lengths add-data) ([arg (in-list %args)])
>                          ....))  is it ok? Or better 
>(define-values ([(desc-args lengths add-data)
>   (for/lists (a b c) (...) ...))
>
>?
>
>for/fold has no "return" clause. So I have to do
>(call-with-values
>  (for/fold (acc1 acc2 sum) (...) ...)
>  (lambda (acc1 acc2 sum) (reverse acc1) (reverse acc2) sum))
>
>And for/list doesn't allow to add several items to resulting list in one round.
>Even with for/fold it gives
>
>(reverse 
>  (for/fold (acc) ([item (in-list list)])
>     (values (cons (get-val1 item)
>                              (cons (get-val2 item) acc)))))
>
>Awful, especially for reading.
>
>It is clumsy and unobvious.
>In Common Lisp iterate I could do
>(iter (...)
>   (collect (foo) into acc1)
>   (collect (bar) into acc2)
>   (summing ... into sum)
>   (finally (return acc1 acc2 sum)))
>
> http://common-lisp.net/project/iterate/doc/index.html
>
>Is there something similar?
>
>-- 
>Roman Klochkov


-- 
Roman Klochkov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140118/58a868c9/attachment-0001.html>

Posted on the users mailing list.