I'm new in functional programming and I'm trying to create and show a Stack with Haskell. I'd like my program to show me the Stack I'm building with it. This is my code:
module Stack (Stack, empty, push, pop, top, isEmpty) where
data Stack a = EmptyStack | Stk a (Stack a)
push x s = Stk x s
top (Stk x s) = x
pop (Stk _ s) = s
empty = EmptyStack
isEmpty EmptyStack = True
isEmpty (Stk x s) = False`
instance Show a => Show (Stack a) where
show EmptyStack = "|"
show (Stk a b) = (show a) ++ " <- " ++ (show b)
With "show (push 1 empty)" I'd expect an answer (more or less) like: " 1 <- | " But I'm not able to compile the code. When I try it shows the following error:
[1 of 1] Compiling Stack ( Stack.hs, interpreted )
Stack.hs:12:27:
Ambiguous occurrence ‘show’
It could refer to either ‘Stack.show’, defined at Stack.hs:11:9
or ‘Prelude.show’,
imported from ‘Prelude’ at Stack.hs:1:8-12
(and originally defined in ‘GHC.Show’)
Stack.hs:12:47:
Ambiguous occurrence ‘show’
It could refer to either ‘Stack.show’, defined at Stack.hs:11:9
or ‘Prelude.show’,
imported from ‘Prelude’ at Stack.hs:1:8-12
(and originally defined in ‘GHC.Show’)
Failed, modules loaded: none.
I understand the error where program could confuse the "show" from Prelude with an "show" defined by be, but I cannot see that error in my code. Besides, some mates have the same code, and the program works well.
There is something I have to change or I have missed?
Thanks!