[Haskell-cafe] showing a user defined type

Ryan Ingram ryani.spam at gmail.com
Mon May 18 22:02:00 EDT 2009


Unfortunately, you can't derive Show on Chain as defined, because it
contains a function:

> data Chain = Link Int (Int -> Chain)

You can write this:

> instance Show Chain where
>    show (Link n _) = "Link " ++ show n ++ " <fn>"

Or you can make a dummy "Show" instance for functions:

> instance Show (a -> b) where show _ = "<fn>"
> data Chain = Link Int (Int -> Chain) deriving Show

One question: Do you expect to ever call the function with a different
value?  For example:

otherChain :: Chain
otherChain = case (ints 0) of Link _ f -> f 100

If not, you can replace Chain entirely by [Int], due to laziness,
something that's not possible in ML.  (Although you can get the same
result in ML by using (int * (() -> chain)) instead.

  -- ryan

On Mon, May 18, 2009 at 6:36 PM, Brandon S. Allbery KF8NH
<allbery at ece.cmu.edu> wrote:
> On May 18, 2009, at 21:19 , michael rice wrote:
>
> *Main> :t ints 0
> ints 0 :: Chain
> *Main> ints 0
>
> <interactive>:1:0:
>     No instance for (Show Chain)
>
> In general, you want to append
>     deriving Show
> to your types.  You may also want to be able to input them in ghci, so
> instead say
>     deriving (Show, Read)
> --
> brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
> system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
> electrical and computer engineering, carnegie mellon university    KF8NH
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


More information about the Haskell-Cafe mailing list