<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Hi Ryan,<br><br>I'm afraid you've lost me. Maybe if you showed how this would be used in ML I would get the picture.<br><br>Michael<br><br>--- On <b>Tue, 5/19/09, Ryan Ingram <i><ryani.spam@gmail.com></i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Ryan Ingram <ryani.spam@gmail.com><br>Subject: Re: [Haskell-cafe] showing a user defined type<br>To: "michael rice" <nowgate@yahoo.com><br>Cc: "Brandon S. Allbery KF8NH" <allbery@ece.cmu.edu>, haskell-cafe@haskell.org<br>Date: Tuesday, May 19, 2009, 2:40 PM<br><br><div class="plainMail">On Tue, May 19, 2009 at 7:07 AM, michael rice <<a ymailto="mailto:nowgate@yahoo.com" href="/mc/compose?to=nowgate@yahoo.com">nowgate@yahoo.com</a>> wrote:<br>> A little further along in "The Little MLer" the ints function is
replaced by<br>> other functions like primes and fibs, which also return Links:<br>><br>> fun primes(n)<br>> = if is_prime(n+1)<br>> then Link(n+1,primes)<br>> else primes(n+1)<br>><br>> fun fibs(n)(m)<br>> = Link(n+m,fibs(m))<br>><br>> which are passed to chain_item:<br>><br>> fun chain_item(n,Link(i,f))<br>> = if eq_int(n,1)<br>> then i<br>> else chain_item(n-1,f(i))<br>><br>> which can be called to request the nth (12th) prime number beginning at 1.<br>><br>> - chain_item(12,primes(1));<br>> GC #0.0.0.1.3.61: (1 ms)<br>> val it = 37 : int<br>> -<br>><br>> So I guess the answer to your question about whether the function is ever<br>> called with a different value may be, yes.<br><br>Actually, it's not calling it with another value; notice that<br>chain_item calls f(i), with i coming directly from the
chain.<br>Consider this alternate definition:<br>(I'm not sure the syntax is exactly right, but you get the idea)<br><br>datatype chain =<br> Link of (int * ( unit -> chain ))<br><br>fun intsFrom(n) = fun unit => (n, intsFrom (n+1))<br>fun ints(n) = intsFrom n ()<br><br>Now you *can't* call the function embedded in the link with another value.<br><br>fun chain_item(n,Link(i,f))<br> = if eq_int(n,1)<br> then i<br> else chain_item(n-1,f unit)<br><br>And this type for "chain" is almost the same as [Int] in Haskell, due<br>to laziness.<br><br> -- ryan<br></div></blockquote></td></tr></table><br>