[Haskell-cafe] showing a user defined type
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:
= if is_prime(n+1)
which are passed to chain_item:
= if eq_int(n,1)
which can be called to request the nth (12th) prime number beginning at 1.
GC #0.0.0.1.3.61: (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.
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
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe