[Haskell-cafe] showing a user defined type

michael rice nowgate at yahoo.com
Tue May 19 10:07:30 EDT 2009


I had put together something similar to your first suggestion but tried to use PutStrLn(Show...). I'd also thought of your second suggestion about a dummy show for functions.

A little further along in "The Little MLer" the ints function is replaced by other functions like primes and fibs, which also return Links:

fun primes(n)
  = if is_prime(n+1)
 then Link(n+1,primes)
 else primes(n+1)

fun fibs(n)(m)
  = Link(n+m,fibs(m))

which are passed to chain_item:

fun chain_item(n,Link(i,f))
  = if eq_int(n,1)
  then i
  else chain_item(n-1,f(i))

which can be called to request the nth (12th) prime number beginning at 1.

- chain_item(12,primes(1));
GC #   (1 ms)
val it = 37 : int

So I guess the answer to your question about whether the function is ever called with a different value may be, yes.


--- On Mon, 5/18/09, Ryan Ingram <ryani.spam at gmail.com> wrote:

From: Ryan Ingram <ryani.spam at gmail.com>
Subject: Re: [Haskell-cafe] showing a user defined type
To: "Brandon S. Allbery KF8NH" <allbery at ece.cmu.edu>
Cc: "michael rice" <nowgate at yahoo.com>, haskell-cafe at haskell.org
Date: Monday, May 18, 2009, 10:02 PM

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090519/37fa0cb0/attachment.html

More information about the Haskell-Cafe mailing list