[racket] for/hash and for/list: adding nothing, adding more than one thing

From: Pierpaolo Bernardi (olopierpa at gmail.com)
Date: Fri Mar 22 12:23:56 EDT 2013

On Thu, Mar 21, 2013 at 11:55 PM, Tim Nelson <tbnelson at gmail.com> wrote:
> Dear All,
>
> I've recently started hacking Racket again, and am loving the for/*
> constructs.
> Awesome stuff. However, I have two questions about for/hash.
>
> (1) How can I *not* add a hash entry for a given iteration?
>
> When I use for/list, can I abuse the nature of empty and append* (I cringe
> as I write this):
>
> (append* (for/list ([i '(1 2 3 4 5)])
>              (cond [(even? i) (list (* i 100))]
>                    [else empty])))
>
> Surely there is a better way to do this for for/list, and a way in general
> for for/hash?
>
> (2) How can I add multiple entries (that is, >1 key-value pair) in a given
> iteration?
>
> Again, using for/list I can cheat via append*:
>
> (append* (for/list ([i '(1 2 3 4 5)])
>            (list (* i 100) (* i 200))))
>
> What's the right way to do this in for/hash?

To both of the questions, the answer is for/fold!

Cheers
P.

Posted on the users mailing list.