[Haskell] Re: rekursive array problem

Peter Simons simons at cryp.to
Mon May 2 11:52:44 EDT 2005


Andreas Fuertig writes:

 > fillArray ["a"]
 > should be something like this:
 > [[("a",True)],[("a",False)]]

A pretty generic solution using the "Bounded" and "Enum"
type classes to calculate the list of all values for a given
type would be:

enumAll :: (Bounded a, Enum a) => [a]
enumAll = [ minBound .. maxBound ]

fillArray :: (Bounded b, Enum b) => [a] -> [(a,b)]
fillArray xs = [ (x,y) | x <- xs, y <- enumAll  ]

In GHCi, you can use these functions like this:

 | *Main> enumAll :: [Bool]
 | [False,True]
 |
 | *Main> fillArray "abc" :: [(Char, Bool)]
 | [('a',False),('a',True),('b',False),('b',True),('c',False),('c',True)]

Peter



More information about the Haskell mailing list