[Haskell-beginners] No instance for (Show a)

Ovidiu Deac ovidiudeac at gmail.com
Sun Jul 31 11:19:36 CEST 2011


For some reason ghc complains about not being able to call show on an
Integer (?!?!?)

Please enlighten me!

ovidiu

This is the hspec:
    it "shows one element"
        ( show (push 1 EmptyStack) ≡ "Stack(1)")

...this is the code:

data Stack a =
    EmptyStack |
    StackEntry a (Stack a)
    deriving(Eq)


instance Show (Stack a) where
    show s =
        "Stack(" ⊕ (showImpl s) ⊕ ")"
        where
            showImpl EmptyStack = ""
            showImpl (StackEntry x _) = show x

...and this is the error:

src/Stack.hs:12:22:
    No instance for (Show a)
      arising from a use of `showImpl'
    In the first argument of `(++)', namely `(showImpl s)'
    In the second argument of `(++)', namely `(showImpl s) ++ ")"'
    In the expression: "Stack(" ++ (showImpl s) ++ ")"



More information about the Beginners mailing list