[Haskell-cafe] Is lazyness make big difference?

Nick nick.linker at rambler.ru
Thu Feb 15 06:22:19 EST 2007


Hi all,

(Another topic stolen from a Russian forum discussion).

As everyone know, there are lot of strict languages, that have 
possibilities to "switch on" lazy evaluation when needed.

But the examples that I saw on Haskell, there was not much use of lazy 
evaluation, often there were just several lazy points, and the rest 
could be done strictly without loss of generality. For example, in 
finding primes:

    main        = print primes
    primes      = 2:filter is_prime [3,5..]
    is_prime n  = all (\p-> n `mod` p /= 0) (takeWhile (\p-> p*p<=n) primes)

We can rewrite this in strict languages with lazy constructs. For 
example, in Scala (of course stream is not only lazily evaluated thing 
there)

    def main(args: Array[String]): Unit = {
        val n = Integer.parseInt(args(0))
        System.out.println(primes(ints(2)) take n toList)
    }

    def primes(nums: Stream[Int]): Stream[Int] =
        Stream.cons(nums.head,
            primes ((nums tail) filter (x => x % nums.head != 0)) )

    def ints(n: Int): Stream[Int] =
        Stream.cons(n, ints(n+1))

I think the Haskell solution is more compact due to syntactic sugar, 
curring and "parentheses-free-ness", *not* lazy evaluation.

According to one guy's analogy: the Real World is strict - in order to 
drink tea, you have to put the cattle on the fire, wait until water 
boils, brew tea and then drink. Not the cattle is put on the fire, water 
boils and the tea is brewed when you take the empty cup to start 
drinking. :-)

The question is the following: how big the gap between strict languages 
with lazy constructs and Haskell? Does the default lazyness have 
irrefutable advantage over default strictness?

Thank you for the attention.

With best regards,
Nick.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070215/42323c8e/attachment.htm


More information about the Haskell-Cafe mailing list