[MLton] Fixed points in SML

Stephen Weeks sweeks at sweeks.com
Wed Aug 30 06:13:17 PDT 2006


> I think the problem has more to do with laziness.

I think so too.  And the nice thing about strict languages like SML is
that one can always get laziness, by introducing a thunk, in the
occasional case that one needs it.

> I am no Haskell programmer, but
>    val Y : ((unit -> 'a) -> 'a) -> 'a = fn g => (fn x => x (T x)) (fn  
> (T x) => fn () => g (x (T x))) ()
> seems to do much the same thing that a lazy language would.

That's my favorite version.  One could do it with ref cells too.

   val Y: ((unit -> 'a) -> 'a) -> 'a =
      fn g =>
      let
         val r = ref (fn () => raise Fail "Y")
         val a = g (fn () => !r ())
         val () = r := (fn () => a)
      in
         a
      end

  val fact = Y (fn fact => fn n => if n=0 then 1 else n * fact () (n-1))
  val 120 = fact 5



More information about the MLton mailing list