[Haskell-cafe] Lazy evaluation from "Why Functional programming
matters"
Hemanth Kapila
saihemanth at gmail.com
Tue Oct 5 09:00:59 EDT 2010
Hi,
Let us try to rewrite the code in a more java-esque syntax:
It translates to something like the below generic method. Correct?
static <T> T function(IBoundsCheck<T> within, Delta<T> eps, Iterator<T>
iterator, T initValue){
T currVal = initVal;
while(iterator.hasNext()){
T nextVal = iterator.next();
if(within.verify(delta, eps, currVal, nextVal))
return currVal;
currVal = nextVal
}
}
I have not tested it but I think this is a fair translation of the code.
(For instance, by using an appropriate implementation of IBoundsCheck, I
will be able to implement the 'relativeSqrt' functionality of the example).
But this IS still a lazy evaluation. By passing an iterator instead of a
list as the third argument of the static method, I achieved 'laziness'.
In the example, the laziness is in the way we are iterating over the
sequence of values [a0,f(a0), f(f(a0)),...] and so on and not on when the
runtime evaluates appropriate values.
Just that having to write,
(repeat (next N) a0)
is (take 1000 (repeat 1)) times more intuitive and convenient than having to
implement the Iterator for T or implementing a true-while loop.
/Hemanth K
On Tue, Oct 5, 2010 at 4:50 PM, C K Kashyap <ckkashyap at gmail.com> wrote:
> Hi All,
>
> I was going through the paper's "lazy evaluation" section where the
> square root example is given. It occurred to me that one could
> implement it in a modular way with just higher order functions
> (without the need for lazy evaluation that is).
>
>
> function f (within, eps, next, a0){
> while(true){
> a1=next(a0);
> if(within(a0,a1,eps)return a0;
> a0=a1;
> }
> }
>
> Is this not the case?
>
> --
> Regards,
> Kashyap
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20101005/9978c2f7/attachment.html
More information about the Haskell-Cafe
mailing list