Hello Everyone! I am relatively new to Haskell (actually it's my second day) and I have the following problem: If I define a new datatype, for example a Binary tree, how can i use the show function to put an instance of my datatype into a String representation. I have seen in different Tutorials that there is a way for overloading the show function for my own type, and I tried out, what was presented in the tutorials, but it didn't work. Does somebody know, how to manage this? Here my attempts: data Entry a = EmptyEntry | MakeEntry a a showEntry :: (Show a) => Entry a -> String showEntry EmptyEntry = "Empty" showEntry MakeEntry a b = show a ++ ": " ++ show b what am I doing wrong? I have seen this way on some tutorials. btw: I am working with Hugs98 thank you in advance for your help Stephan
On Sep 10, 2004, at 3:37 PM, Stephan Zaubzer wrote:
Hello Everyone!
I am relatively new to Haskell (actually it's my second day) and I have the following problem: If I define a new datatype, for example a Binary tree, how can i use the show function to put an instance of my datatype into a String representation. I have seen in different Tutorials that there is a way for overloading the show function for my own type, and I tried out, what was presented in the tutorials, but it didn't work. Does somebody know, how to manage this?
Here my attempts:
data Entry a = EmptyEntry | MakeEntry a a
You could make use of the default show by simply deriving Show: data Entry a = EmptyEntry | MakeEntry a a deriving Show Otherwise you need to provide an instance of Show for your type, as such: instance Show a => Show (Entry a) where show EmptyEntry = "Empty" show (MakeEntry x y) = show x ++ ": " ++ show y (untested code) -marius
Stephan Zaubzer writes:
Here my attempts:
data Entry a = EmptyEntry | MakeEntry a a
showEntry :: (Show a) => Entry a -> String showEntry EmptyEntry = "Empty" showEntry MakeEntry a b = show a ++ ": " ++ show b
what am I doing wrong? I have seen this way on some tutorials.
I believe that if you looked cautiously at the system response you would guess yourself. Parentheses missing, it should be showEntry (MakeEntry a b) = ... Jerzy Karczmarczuk ... who has a pleasure of correcting thousands students' programs per year, so this kind of error burns his eyes...
Thank you to all 3 of you! That helped a lot! karczma wrote:
Stephan Zaubzer writes:
Here my attempts: data Entry a = EmptyEntry | MakeEntry a a showEntry :: (Show a) => Entry a -> String showEntry EmptyEntry = "Empty" showEntry MakeEntry a b = show a ++ ": " ++ show b what am I doing wrong? I have seen this way on some tutorials.
I believe that if you looked cautiously at the system response you would guess yourself. Parentheses missing, it should be showEntry (MakeEntry a b) = ... Jerzy Karczmarczuk ... who has a pleasure of correcting thousands students' programs per year, so this kind of error burns his eyes...
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
participants (3)
-
karczma -
Marius Nita -
Stephan Zaubzer