[Haskell-beginners] Enum and Bounded in generic type

Philippa Cowderoy flippa at flippac.org
Tue Dec 30 12:17:37 EST 2008


[-cafe left out]

On Tue, 2008-12-30 at 17:12 +0000, raeck at msn.com wrote:
> Hi, how can I make the following code work? (show all the possible values of 
> a type 'a')
> 
> > showAll :: (Eq a, Bounded a, Enum a) => a -> [a]
> > showAll a = [minBound..maxBound]::[a]
> 
> it keeps saying that I could fix the problem by adding (Enum a) and (Bounded 
> a) the the context, but I failed when I try.
> 

The problem is the inner annotation, ::[a]. It's not the same a as in
(Eq a, Bounded a, Enum a) => a -> [a], and in Haskell98 it can't be. I'm
not surprised that's confused you! If you remove the inner annotation it
should work fine though.

You can go a step further and make showAll a value rather than a
function:

showAll = [minBound..maxBound]

(I've left out the type annotations, but you can put ":t
[minBound..maxBound]" into ghci or hugs if you want one)

-- 
Philippa Cowderoy <flippa at flippac.org>
I left the snappy .sigs on the other computer



More information about the Beginners mailing list