[racket] generating 0 or more than 1 additions in the body of for/list, for/vector, for/hash

From: John Clements (clements at brinckerhoff.org)
Date: Fri Apr 15 15:18:28 EDT 2011

On Apr 15, 2011, at 12:11 PM, harsha reddy wrote:

> When using  the various versions of for like for/list; I often encounter situations where the body must not add any value to put into the generated list  (useful when one needs to filter and map simultaneously) or must generate more than value. I guess the way to do this would be to treat the result of the body as a list of additions and then concatenate all these lists. Something similar would be needed for for for/hash(taking care of the multiple values issue),for/and, for/vector etc.
>   I just wanted to know, because this seems like a common enough situation, if there are some standard commands or libraries which implement the modified versions of these commands.

It sounds like you might be interested in the #:when clause of a for/list; this gives you half of what you want, but doesn't work when you want to add multiple elements to the list.

For those, you can use for/fold, which makes the list extension explicit. Honestly, though, the code is probably more readable if you just wrap an (apply append ...) around the result of the loop.

Not exactly what you were asking for, but not too far off....

John Clements

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4624 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20110415/50c203f9/attachment.p7s>

Posted on the users mailing list.