
10 Jul
2006
10 Jul
'06
1:14 p.m.
Johan Grönqvist wrote:
I would like use a list (as stack) that can contain several kinds of values.
data Element = Int Int | Float Float | Func : Machine -> Machine | ...
Now I would like to have this type be an instance of the class Show, so that I can see what the stack contains in ghci.
"deriving Show" is impossible as Func is not instance of Show. Can I make it instance of Show?
Of course you can: instance Show Element where showsPrec p (Int i) = showsPrec p i showsPrec p (Float f) = showsPrec p f showsPrec _ (Func _) = ("<<function>>" ++) ... Udo. -- Alcohol is the anesthesia by which we endure the operation of life. -- George Bernard Shaw