Geeks With Blogs

News

First of all – shout out to Peter Adams – from the feedback I have gotten from him on the last few posts of F# that I have done – my mind has just been expanded.

I did a blog post a few days ago about infinite sequences – I didn’t really understand what was going on with it, and I still don’t really get it – but I am getting closer.

In Peter’s last comment he made mention of Lazy Evaluation. I am ashamed to say that up till then I had never heard about lazy evaluation – how can evaluation be lazy? I mean, I know about lazy loading and that makes sense… but surely something is either evaluated or not! Well… a bit of reading today and I have been enlightened to a point – if you do know of any good articles explaining lazy evaluation please send them to me.

So what is lazy evaluation and why is it useful?

Lazy evaluation is a process whereby the system only computes the values needed and “ignores” the computations not needed. I’m going out on a limb here, but with this explanation in hand, imagine the following C# code…

```public int CalculatedVal()
{
int Val1 = 0;
int Val2 = 0;

for (int Count = 0; Count < 1000000; Count++)
{
Val1++;
}
return Val2;
}```

Normally, even though Val1 is never needed, the system would loop 1000000 times and add 1 to the current value of Val1. Imagine if the system realized this and so just skipped this segment of code and instead did the following….

```public int CalculatedVal()
{
int Val1 = 0;
return Val2;
}```

A massive saving in computation and wasted effort. Now I am pretty sure it isn’t as simple as this but I think this is the basic idea. For a more detailed explanation of lazy evaluation in c#, Pedram Rezei has a wonderful post on lazy evaluation and makes some C# comparisons. I am not going to take any thunder from him by repeating everything he said since I think he did such a good job of explaining it himself.

What I am interested in though is how in F# do you tell something to have lazy evalution, and how do you know if something will be eager or lazy by looking at it.

I found this post was useful.

From reading around F# by default uses eager evaluation unless explicitly told to use lazy evaluation. One exception to this is sequences, which are lazy by default.

From my understanding of F# because of its declarative nature, most of the actual code you are declaring properties and rules – very little code is actually saying do this right now - but when it comes to a “do this” code section, it then evaluates and optimizes code and applies the rules.

So props to lazy evaluation and its optimizations…

Related Posts on Geeks With Blogs Matching Categories

Comments on this post: Lazy Evaluation – Why being lazy in F# blows my mind!

# re: Lazy Evaluation – Why being lazy in F# blows my mind!
Mark,

Generally speaking, the compiler will actually do exactly as you describe, and it will remove that whole loop. A better example of lazy evaluation would be something like:

public int fun(){
console.writeline("I'm being evaluated!");
return 1;
}

public void main(string[] args){
console.writeline(fun());
console.writeline(fun());
}

under strict evaluation, you'd see:
I'm being evaluated!
1
I'm being evaluated!
1

whereas under lazy evaluation you'd see:
I'm being evaluated!
1
1

because the result of calling fun is already known the second time around, and therefore doesn't need calling again.

Hope this helps
Alec
Left by Alec Zorab on Apr 19, 2010 10:31 PM

# re: Lazy Evaluation – Why being lazy in F# blows my mind!
Alec, thank for the example... that explains it even further. great!
Left by Mark on Apr 20, 2010 2:06 AM

# re: Lazy Evaluation – Why being lazy in F# blows my mind!
Actually, for the example you give, an optimizing compiler wouldn't even generate the code for a loop. It would simply produce the answer.
But that is nothing to do with lazy evaluation.
I ran into this problem some time ago trying to write benchmarking software.
Left by Peter Adams on Apr 20, 2010 2:27 AM

# re: Lazy Evaluation – Why being lazy in F# blows my mind!
You've almost got it.

When I was learning Haskell, my lecturer gave a good example. If you have a variable in C# (for example) that contains an infinite sequence of numbers (e.g Fibonacci sequence), and you wish to find the first number, the program wouldn't work because it tries to evaluate all the data in the variable before giving you the answer. As the sequence is infinite, you won't ever get the answer.

With Haskell (and F#), you can get the first number (or N) in a sequence of infinite numbers with the Head function, which only evaluates the minimum amount of data necessary to get you the answer.

So an advantage of lazy evaluation is you can process huge data sets without ever having to load it all into memory at once.

Basically it gets the Nth number from a Fibonacci sequence which is generated by a function that generates the sequence from 1 to infinity.

Hope I've got it right, it has been a while since I covered this stuff!
Left by Jon Bridger on Apr 21, 2010 1:05 PM

# re: Lazy Evaluation – Why being lazy in F# blows my mind!
Thanks John - I will check out the link you sent me! Appreciate the feedback!!
Left by Mark on Apr 21, 2010 1:15 PM

# re: Lazy Evaluation – Why being lazy in F# blows my mind!
Have you seen the 'lazy' function in F#? You can wrap code in that if you don't want it to be evaluated immediately and then you need to call 'force' in order for it to be evaluated.

i've not used the language in real situations to know exactly where that would be useful but it seems interesting
Left by Mark Needham on Apr 22, 2010 9:29 AM

# re: Lazy Evaluation – Why being lazy in F# blows my mind!
Thanks Mark - I did see it... I would be interested to see if there are any business cases where it could be used. I know for project Euler it may be a very useful approach to solving problems!
Left by Mark on Apr 22, 2010 9:32 AM

# re: Lazy Evaluation – Why being lazy in F# blows my mind!
What about a lazy function :) .. something as shown below

let sum a b (c:unit) = a + b

let lazySum = sum 1 2
//Lazy sum is lazy now .. it know what all it need to calculate result but //havent calculated yet
let result = lazySum()
Left by Ankur on Aug 25, 2010 3:22 PM