In the past few days, I have researched LazyEvaluation, in performance aspect mostly, wondering from where the performance advantage of LazyEvalutaion emerges.
It's been so unclear to me reading various articles but a very few including
What are the advantages of Lazy Evaluation?
This refers to the evaluation of a syntax tree. If you evaluate a syntax tree lazily (i.e. when the value it represents is needed), you must carry it through the preceeding steps of your computation in its entirety. This is the overhead of lazy evaluation. However, there are two advantages. 1) you will not evaulate the tree unnecessarily if the result is never used,
JavaScript, for instance, implemented if syntax.
if(true)
{
//to evaluate
}
else
{
//not to evaluate
}
In this ordinary scenario, we don't have any performance issue.
To be evaluated is done, not to be evaluated is ignored in the syntax tree.
However, in some recursive loop, for instance, Tak function AKA Tarai function
function tak(x,y,z){ return (x <= y) ? y : tak(tak(x-1, y, z), tak(y-1, z, x), tak(z-1, x, y)); }
Since Eager Evaluation strategy of JS evaluates function(arguments) inevitability, the if - else - not to be evaluated control no longer works, and the number of evaluation step of tak function explodes.
Against to this disadvantage of Eager Evaluation(of JS or other languages), Haskell can evaluate the tak without problems as it is, and some JS library such as lazy.js outperforms in a specific area like functional programming where recursive list management required.
Aside from infinite list, I understand this is the exact reason for performance advantage of LazyEvaluation. Am I correct?