[Haskell-cafe] Combination-lock problem

Lyle Kopnicky lists at qseep.net
Wed Aug 11 17:50:51 EDT 2004


Henning Thielemann wrote:

>On Wed, 11 Aug 2004, Lyle Kopnicky wrote:
>
>  
>
>>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]
>>    
>>
>
>Since there is one zero combination, it should be
>
>  
>
>>combs [] = [[]]
>>    
>>
Ah, yes.  I knew I must be missing something.  That would be a lock 
which has no numbers on it, but can be opened at any time.

>Then you can also remove the definition of combs [n] .
>
>What is the advantage of introducing 'combsr' instead of using 'combs r'
>immediately? 
>  
>
I initially did that to save recalculation, but later shifted things 
around, and now I see there is no need for it.  Thanks.  Here is the 
improved version:

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

- Lyle


More information about the Haskell-Cafe mailing list