# [Haskell-cafe] showing a user defined type

michael rice nowgate at yahoo.com
Tue May 19 19:25:57 EDT 2009

```Hi Ryan,

I'm afraid you've lost me. Maybe if you showed how this would be used in ML I would get the picture.

Michael

--- On Tue, 5/19/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: "michael rice" <nowgate at yahoo.com>
Date: Tuesday, May 19, 2009, 2:40 PM

On Tue, May 19, 2009 at 7:07 AM, michael rice <nowgate at yahoo.com> wrote:
> 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)
>  else primes(n+1)
>
> fun fibs(n)(m)
>
> which are passed to chain_item:
>
>   = 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 #0.0.0.1.3.61:   (1 ms)
> val it = 37 : int
> -
>
> called with a different value may be, yes.

Actually, it's not calling it with another value; notice that
chain_item calls f(i), with i coming directly from the chain.
Consider this alternate definition:
(I'm not sure the syntax is exactly right, but you get the idea)

datatype chain =
Link of (int * ( unit -> chain ))

fun intsFrom(n) = fun unit => (n, intsFrom (n+1))
fun ints(n) = intsFrom n ()

Now you *can't* call the function embedded in the link with another value.

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

And this type for "chain" is almost the same as [Int] in Haskell, due
to laziness.

-- ryan

-------------- next part --------------
An HTML attachment was scrubbed...