[prev in list] [next in list] [prev in thread] [next in thread]
List: haskell-beginners
Subject: Re: [Haskell-beginners] No instance for (Show a)
From: Daniel Fischer <daniel.is.fischer () googlemail ! com>
Date: 2011-07-31 9:37:57
Message-ID: 201107311137.58197.daniel.is.fischer () googlemail ! com
[Download RAW message or body]
On Sunday 31 July 2011, 11:19:36, Ovidiu Deac wrote:
> For some reason ghc complains about not being able to call show on an
> Integer (?!?!?)
No, it complains about not being able to call show on a value of arbitrary
type. Your
instance Show (Stack a) where
says you can show stacks of arbitrary type, so nothing can be assumed about
the element type, but in the implementation you try to call show on the top
element.
To make it work, you have to tell GHC that the elements can be shown,
instance (Show a) => Show (Stack a) where
-- what you have
would work. The "(Show a) =>" part says roughly "if elements of the stack
can be shown, then the stack can be shown".
>
> 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) ++ ")"
>
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic