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

From: David Van Horn (dvanhorn at ccs.neu.edu)
Date: Thu Mar 21 19:12:35 EDT 2013

On 3/21/13 6:55 PM, Tim Nelson wrote:
> (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?
>

You can write this equivalently as:

(for/list ([i '(1 2 3 4 5)]
            #:when (even? i))
   (* i 100))

Which probably answers your for/hash question.

David


Posted on the users mailing list.