[nem-en] Questions about lazy evaluation
mei
mei at work.email.ne.jp
Sun Jun 11 17:14:41 CEST 2006
Hi,
There is a Haskell code.
>| tarai.hs : Haskell sample
main = print (tarai 20 10 5)
tarai :: Int -> Int -> Int -> Int
tarai x y z = if x <= y then y
else tarai (tarai (x-1) y z)
(tarai (y-1) z x)
(tarai (z-1) x y)
|<
Run very fast above code. Because Haskell is lazy evaluation. Then I
notice that Nemerle support lazy evaluation. I try to convert it to
Nemerle.
>| tarai.n : Nemerle sample
using System;
using System.Diagnostics;
using Nemerle;
using Nemerle.IO;
def tarai(x : LazyValue[int], y : LazyValue[int], z : LazyValue[int]) :
LazyValue [int] {
def a : int = x;
if (a <= y) {
y
}
else {
tarai (
tarai (lazy(x-1), y, z),
tarai (lazy(y-1), z, x),
tarai (lazy(z-1), x, y))
}
}
def x = 20;
def y = 10;
def z = 5;
def s = Stopwatch ();
s.Start ();
def a = tarai (lazy(x), lazy(y), lazy(z));
s.Stop ();
printf("tarai(%d, %d, %d) = %d - %ld\n", x, y, z, a, s.ElapsedMilliseconds);
|<
But this is very slow. Can I write faster code like Haskell ?
Thanks.
--
mei <mei at work.email.ne.jp>
More information about the devel-en
mailing list