What do you do when you feel you need a variadic list comprehension?
Posted
by cspyr0
on Stack Overflow
See other posts from Stack Overflow
or by cspyr0
Published on 2010-04-11T06:54:11Z
Indexed on
2010/04/11
7:53 UTC
Read the original article
Hit count: 236
haskell
|list-comprehension
I would like to make a method where I could give it a list of lengths and it would return all combinations of cartesian coordinates up to those lengths. Easier to explain with an example:
cart [2,5]
Prelude> [ [0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4] ]
cart [2,2,2]
Prelude> [ [0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1] ]
A simple list comprehension won't work because I don't know how long the lists are going to be. While I love Haskell's simplicity for many problems, this is one that I could write procedurally (in C or something) in 5 minutes whereas Haskell gives me an aneurysm!
A solution to this specific problem would help me out a lot; I'd also love to hear about your thought processes when tackling stuff like this.
© Stack Overflow or respective owner