[Haskell-cafe] Need some help with an infinite list

Matthew Brecknell haskell at brecknell.org
Wed Jun 17 00:18:23 EDT 2009


Thomas Davie wrote:
> letterCombos = map (:[]) ['a'..'z'] ++ concatMap (\c -> map ((c++) . (: 
> [])) ['a'..'z']) letterCombos
> 
> Not hugely efficient, if you generate the strings in reverse then you  
> can use (c:) rather than ((c++) . (:[])), but that may not be useful  
> to you.
> 
> Bob

I think the following version increases the sharing between the
generated strings, and so might be more space-efficient for consumers
which hold on to a significant number of them:

number :: [a] -> [[a]]
number digits = expand [[]] where
  expand xss = expanded ++ expand expanded where
    expanded = concatMap (\d -> map (d:) xss) digits

binary = number ['0'..'1']
decimal = number ['0'..'9']
alpha = number ['a'..'z']

Regards,
Matthew





More information about the Haskell-Cafe mailing list