haskell prelude
François Sarradin
fsarradin at gmail.com
Thu Jun 21 14:26:02 PDT 2012
The problem here is that Haskell is based on lazy evaluation. This is not
the case of Java, which uses eager evaluation. So 'fibs.eval(x, z + y)' is
evaluated instantly!
To get the desired result in Java, it is necessary to delayed the
evaluation of an expression. To do in FP, you have to "encapsulate" the
expression in a closure with no parameter, like java.util.function.Factory.
This is translated in Java 8 by
Factory<...> delayedExpression = () -> myExpression;
Once you want to evaluate the expression you just have to do
'delayedExpression.make()'.
This may help to build your Fibonacci value generator.
françois-
2012/6/21 Luc Duponcheel <luc.duponcheel at gmail.com>
> Some time ago Sven Eric Panitz posted an experimental haskell prelude
> library to this group (one of his goals being to work with "infinite
> iterables" (right?))
>
>
> So I tried out something similar to
>
> public class Test{
> final private static Function2<Integer, Integer, Iterable<Integer>> fibs =
> (Integer z, Integer y) -> cons(z, cons(y, fibs.eval(y, z + y)));
> public static void main(String[] args){
> System.out.println(take(5, fibs.eval(1, 1)));
> }
> }
>
> but this results in a stack overflow
>
> So my question to Sven (and others (?)) is:
> is there an elegant similar way to define fibs without this issue?
>
>
> thx
>
> Luc
>
> --
> __~O
> -\ <,
> (*)/ (*)
>
> reality goes far beyond imagination
>
>
More information about the lambda-dev
mailing list