Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Stream call-by-need (lazy) vs call-by-name

Tags:

stream

scala

So I understand that call-by-need is simply a memoized version of call-by-name. In Martin Odersky's FP Course on Coursera, in lecture 7.3 (Lazy Evaluation), he mentions that if Streams were implemented using call-by-name, then it could potentially lead to a blowup in computational complexity.

What would be an example of such a blowup?

Call-by-name:

def cons[T](hd: T, tl: => Stream[T]) = new Stream[T] {
  def head = hd
  def tail = tl
  ...
}

Call-by-need:

def cons[T](hd: T, tl: => Stream[T]) = new Stream[T] {
  def head = hd
  lazy val tail = tl
  ...
}
like image 694
platypus Avatar asked May 06 '13 20:05

platypus


People also ask

Is call by name lazy evaluation?

Technically, lazy evaluation means call-by-name plus Sharing. A kind of opposite is eager evaluation. Lazy evaluation is part of operational semantics, i.e. how a Haskell program is evaluated. The counterpart in denotational semantics, i.e. what a Haskell program computes, is called Non-strict semantics.

Does Scala use lazy evaluation?

Lazy Evaluation in Scala Haskell is a functional programming language that uses lazy evaluation by default.


1 Answers

For example the Fibonacci series, implemented by adding the previous two elements to form the successor. Without memoization, that would have a linear slowdown (and stack growth) in the length of the sequence:

scala> lazy val fib: Stream[Int] = Stream.cons(0,
     | Stream.cons(1, fib.zip(fib.tail).map(p => p._1 + p._2)))
fib: Stream[Int] = Stream(0, ?)

Example lazily (-sic-) copied from this blog

like image 175
0__ Avatar answered Nov 10 '22 00:11

0__