Checking if a Stack is Empty

Hello, I have this stack data structure : data Stack = Empty | Element Char Stack deriving Show I want to check if it is equal to "Empty" When I try something like this : "a = Empty" or "a = (Empty)" in a haskell file and then write this on ghci : "a = Empty" I get this : <interactive>:1:0: No instance for (Eq Stack) arising from a use of `==' at <interactive>:1:0-11 Possible fix: add an instance declaration for (Eq Stack) In the expression: a == (Empty) In the definition of `it': it = a == (Empty) I don't know how to fix this. Can you help me so that I can check if a stack is Empty without getting this error? Thank you.

On Tue, Mar 12, 2013 at 10:48 AM, doaltan
I have this stack data structure : data Stack = Empty | Element Char Stack deriving Show I want to check if it is equal to "Empty" When I try something like this : "a = Empty" or "a = (Empty)" in a haskell file and then write this on ghci : "a = Empty"
Have you studied any Haskell tutorials yet? Usually you want to use a pattern match, not an equality test; if you must for some reason use an equality test, you need an Eq instance (as the error message tells you). -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Hi, Dnia 2013-03-12, wto o godzinie 14:48 +0000, doaltan pisze:
Hello, I have this stack data structure :
data Stack = Empty | Element Char Stack deriving Show I want to check if it is equal to "Empty" When I try something like this :
"a = Empty" or "a = (Empty)" in a haskell file and then write this on ghci : "a = Empty" I get this :
<interactive>:1:0: No instance for (Eq Stack) arising from a use of `==' at <interactive>:1:0-11 Possible fix: add an instance declaration for (Eq Stack) In the expression: a == (Empty) In the definition of `it': it = a == (Empty) I don't know how to fix this. Can you help me so that I can check if a stack is Empty without getting this error?
First, it looks like you should read 2 or 3 times this book: http://learnyouahaskell.com/ Second, you should use a function like this: isEmpty :: Stack -> Bool isEmpty Empty = True isEmpty _ = False Emanuel

This chapter of Real World Haskell exactly explains the problem you're having:
http://book.realworldhaskell.org/read/using-typeclasses.html
(The short answer is, you need an `Eq` instance for the data type
you've created (`Stack`). You can just use `deriving`.)
Tom
On Tue, Mar 12, 2013 at 7:48 AM, doaltan
Hello, I have this stack data structure : data Stack = Empty | Element Char Stack deriving Show I want to check if it is equal to "Empty" When I try something like this : "a = Empty" or "a = (Empty)" in a haskell file and then write this on ghci : "a = Empty" I get this : <interactive>:1:0: No instance for (Eq Stack) arising from a use of `==' at <interactive>:1:0-11 Possible fix: add an instance declaration for (Eq Stack) In the expression: a == (Empty) In the definition of `it': it = a == (Empty) I don't know how to fix this. Can you help me so that I can check if a stack is Empty without getting this error? Thank you.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (4)
-
Brandon Allbery
-
doaltan
-
Emanuel Koczwara
-
Tom Murphy