From: Julian Snitow Date: 2003-03-25T02:59:13+09:00 Subject: Re: Iterate over two lists in parallel Gavin Sinclair wrote: > On Monday, March 24, 2003, 3:35:23 PM, Julian wrote: > > >>Or have I misinterpreted the problem? It seems too trivial to be a >>"show me that this is possible" kind of problem... :-( > > > Thanks for the solution. You didn't misunderstand the problem, nor > did I doubt it was possible. But Jim mentioned that walking two lists > in parallel was a special case that Ruby's iterators don't handle very > gracefully. So I wanted to see what "the Ruby way" was. > > Thanks also for reinforcing that I shall never understand > continuations! > > Gavin > > In all fairness, my continuation-based solution was much more convoluted than it had to be. I was just having so much fun with them I couldn't help myself... ;-) Ok, the basics of continuations: A continuation (in theory) represents "the rest of the program" from the point that it's created. Imagine you're walking down the street and you come to a fork in the road. You don't know whether to go left or right, so you generate a "continuation". Road Left-or-right? generate a continuation called "choice" | \--Go left | |---walk around | |---find a mean dog | \---get bitten by the mean, terribly hungry dog | \---that wasn't a good idea. | \---tell "choice" that left was a bad idea | |----according to "choice", left was a bad idea \----let's go right instead. Now, the real magic of continuations happens when you have more than one of them. Say when you got bitten by the dog, you generated another continuation called "got bitten", before telling "choice" that left was a bad idea. So, you're walking along, un-bitten, since you went right at the fork in the road after all, and suddenly you're hit by an oncoming train. You decide that getting bitten by the dog was preferable to this, so you kindly ask the continuation named "got bitten" to take you back to the alley where you were lying bleeding from the dog bite, but at least hadn't been hit by a train. ;-) The moral of the story: When in doubt, just stay home. For a more in-depth look at continuations, see "Run, Lola, Run", "The Legend of Zelda: Ocarina of Time", and "Back to the Future: Part II" :-)