Wirawan Winarto

Microsoft Student Partner
See also: Other Geeks@INDC

The Evaluation Is Not So F#-ing Lazy

Everytime I write F# down. It looks like a swear.

In functional programming languages, lazy evaluation is a well-known feature. The opposite of lazy evaluation is a strict evaluation. In a strict evaluation strategy, the innermost expressions are evaluated first. While in a non-strict evaluation strategy, the outermost expressions are evaluated first. Look at this easy example :

The advantage of strict evaluation is clear, it is more efficient since all functions are only evaluated once. Meanwhile, in non-strict evaluation, the functions like g(1,3) above sometimes are evaluated twice (or more). Inversely, non-strict evaluation is considered more expressive, for example it supports infinitive data structures, etc.

Hence people came up with a solution called lazy evaluation. Lazy evaluation is a form of non-strict evaluation, but it uses memoization, to work efficiently. Memoization is a way to remember the result produced by an evaluation. The results produced are shared across the functions so that it does not need to evaluate the same expression twice. Another advantage of lazy evaluation is it only evaluates a function that is necessary, while in strict evaluation strategy the same program may loop forever or crash during the useless calculations. By avoiding unnecessary calculations, lazy evaluation can work faster.

There are some disadvantages of lazy evaluations as well. One of them is the difficulty to predict the complexity of program. It is because sometimes we cannot predict which functions are evaluated and which functions are not. Thus it is also makes errors harder to debug.

Strict evaluation is used by old functional programming languages, like Lisp and ML. While lazy evaluation is used by newer functional programming languages, such as Miranda and Haskell. How about F#? Fortunately, F# gives us both alternatives to choose. F# does not do lazy evaluation by default. It allows us to easily create a lazily evaluated function by adding the Lazy keyword. It also allows us to build a lazily evaluated functions on the top of strictly evaluated functions. Look at the meaningless code below on how to declare a laziness :

This is just information, not tutorial. The manuals will help you more.

Share this post: | | | |

Comments

Don Syme said:

Got this one late at night.

Note you can just use "lazy 10" or "lazy (10+10)" etc.

Cheers!

Don

# December 17, 2007 6:22 AM

augustss said:

I'm sorry, lazy evaluation has not been implemented the way you describe by anyone serious in the last 35 years.  Use graph reduction.

# December 17, 2007 7:49 AM

Norman Sasono said:

"it uses memoization, to work efficiently. Memoization is a way to remember the result produced by an evaluation. The results produced are shared across the functions so that it does not need to evaluate the same expression twice." ---> Hmm, then it uses the "Dynamic Programming" Algorithm Design Technique.

# December 17, 2007 4:49 PM

wirawan said:

@dsyme

wow, mr don syme!

thanks! it helps. actually i am just learning. so I am not experienced yet. :)


@augustss

that sounds interesting...

can you explain it more? :)


@norman

whoaah! i guess so. ^^

# December 20, 2007 6:33 PM