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

From: J. Ian Johnson (ianj at ccs.neu.edu)
Date: Mon Mar 25 09:41:56 EDT 2013

Let me know how you like my library. I'm thinking of adding some more ergonomics to it:
1) support right folds so a for/list-like comprehension has better allocation behavior.
2) add a keyword for-clause for starting an inner iteration so you don't have to do this:
(for ([e0 (in-list l0)]
      [e1 (in-list l1)])
  (for ([e2 (in-list l2)]
        [e3 (in-list l3)])
    ...body...))
but instead you can do this:
(for ([e0 (in-list l0)]
      [e1 (in-list l1)]
      #:start-inner-loop ;; suggest a better name please
      [e2 (in-list l2)]
      [e3 (in-list l3)])
  ...body...))

I seem to remember that for* is not equivalent to successive nesting of for forms, but I can't remember how I reached this conclusion.

-Ian
----- Original Message -----
From: "Tim Nelson" <tbnelson at gmail.com>
To: "Pierpaolo Bernardi" <olopierpa at gmail.com>
Cc: "users" <users at racket-lang.org>
Sent: Monday, March 25, 2013 9:16:44 AM GMT -05:00 US/Canada Eastern
Subject: Re: [racket] for/hash and for/list: adding nothing, adding more than one thing


Thanks everyone for the useful feedback. I'll check out all three options. :-) 


On Fri, Mar 22, 2013 at 12:23 PM, Pierpaolo Bernardi < olopierpa at gmail.com > wrote: 




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. 


____________________
  Racket Users list:
  http://lists.racket-lang.org/users

Posted on the users mailing list.