Sunday, August 16, 2009

Simple analogy for lazy evaluation

In my talk on Friday, I was explaining Haskell's laziness using the following example:
evens (_:x:xs) = x : evens xs
I used promises and thunks to show that the recursive application of evens xs is not evaluated immediately and that we could define the unbounded list of all positive even numbers with evens [1..] without running off into an infinite loop. Some in the mostly imperative-minded audience were still confused.

Someone asked, “Is it kind of like a recipe?” Again my wife's oatmeal cream pies found a fun place in Haskell and this time provided a helpful analogy!

In our pantry we have ingredients such as oatmeal, sugar, and so on. She has her trusty recipe. As long as we provide ingredients and are willing to wait, we can get as many oatmeal cream pies as we want.

The analogy is imperfect. Diminishing marginal utility doesn't weigh on a CPU's execution of programs, but smiling, appreciative faces and delighted exclamations of "Mmm!"—although powerful—may ultimately need to be augmented with shoe-shopping gift certificates to keep the party going.

No comments: