[Haskell-cafe] Defining show for a function type.

Udo Stenzel u.stenzel at web.de
Mon Jul 10 13:14:16 EDT 2006

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>>" ++)

Alcohol is the anesthesia by which we endure the operation of life.
                -- George Bernard Shaw
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20060710/27fa781c/attachment.bin

More information about the Haskell-Cafe mailing list