"interact" behaves oddly if used interactively

Thomas Hallgren hallgren at cse.ogi.edu
Thu Oct 2 17:06:56 EDT 2003


John Meacham wrote:

>On Thu, Oct 02, 2003 at 12:08:07PM +0100, Nicholas Nethercote wrote:
>  
>
>>This is because strict evaluation is always easy to understand.
>>Lazy/non-strict evaluation can be non-intuitive/confusing/surprising, even
>>for people who are used to it.
>>    
>>
>what is easy or confusing and what becomes intuitive is learned and
>depends a lot on background..
>
>... IMHO lazy semantics can become just as intuitive and easy
>as strict or mathematical semantics.
>        John
>  
>
I agree.

I once got confused by strict evalution, even though I had never seen a 
lazy functional language.

This happened when I was exposed to Standard ML as an undergraduate 
student. Before then, I had been learning programming using imperative 
languages like Pascal and Modula-2. One if the things I liked in SML was 
the conciseness offered by if-then-else *expressions*, e.g.,

    fun fac n = if n=0 then 1 else n*fac(n-1)

as compared to the more verbose if-then-else *statements* I had used in 
other languages. But then I had they idea that I could do the same in 
Module-2 simply by defining an if-then-else function!

PROCEDURE IfThenElse(b:BOOLEAN; t,e:INTEGER):INTEGER;
BEGIN
  IF b THEN RETURN t ELSE RETURN e END
END IfThenElse;

PROCEDURE fac(n:INTEGER):INTEGER;
BEGIN
  RETURN IfThenElse(n=0,1,fac(n-1))
END fac;

But I was surprised to notice that this implementation of the factorial 
function looped. I soon realized what the problem was: the IfThenElse 
function is too strict! I was very disappointed...

So, for over ten years now, I have done most of my programming in lazy 
functional languages. I am sure I would run into occasional surprises if 
I were to switch back to a strict language.

-- 
Thomas H

"Delay is preferable to error."
  - Thomas Jefferson (3rd President of the United States)




More information about the Haskell-Cafe mailing list