[Haskell-cafe] Combination-lock problem

Lyle Kopnicky lists at qseep.net
Wed Aug 11 12:49:45 EDT 2004


Here's my version:

combs [] = []
combs [n] = [[i] | i <- [0..n]]
combs (n:r) = let combsr = combs r in [i:cr | i <- [0..n], cr <- combsr]

- Lyle

Florian Boehl wrote:

>Hi,
>
>I'ld like to generate a list (of lists) that contains all combinations
>of natural numbers stored in another list. It should work like a
>combination-lock. E.g.:
>
>[2,2] -> [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]
>
>If I know the length 'l' of the 'locklist', I can solve the
>problem via generators. E.g.:
>
>l = 2: [[a,b] | a <- [0..locklist!!0], b <- [0..locklist!!1]]
>
>But if the length is unknown (because it's dynamic) this solutions (of
>course) fails. Is it possible to solve this problem in haskell in an
>elegant way?
>
>Thanks
>
>Flo
>_______________________________________________
>Haskell-Cafe mailing list
>Haskell-Cafe at haskell.org
>http://www.haskell.org/mailman/listinfo/haskell-cafe
>  
>



More information about the Haskell-Cafe mailing list