Hacker News new | ask | show | jobs
by wwwwwwwwww 4384 days ago
honest question: is there any reason to add lazy evaluation to c++ other than to just add more features to the language?

lazy evaluation is one of those features where i have absolutely no idea what benefit it actually provides

4 comments

>lazy evaluation is one of those features where i have absolutely no idea what benefit it actually provides

Good question.

Let's say you have a program like this little python example:

    inputs = [raw_read() for i in range(1000)] # Read numbers from command line
    numbers = map(int, inputs) # Turn them into integers
    for num in numbers: # Check for 42
      if num == 42:
        return true
    return false # It's not there
If you use lazy evaluation, each item in the "map" object will only be calculated as needed. So if the first item in the "inputs" list is "42", it will only do a single string-to-integer conversion.

If you use greedy evaluation, it will map all strings to integers at once, which might be wasteful.

There are other benefits as well; this is the most obvious.

But in a language with mutating side effects, and iterators, I'm not sure what this buys you over:

    for (auto& num = file.line_iterator(); num != file.end(); num++) {
        if (atoi(num) == 42)
            return true;
    return false;
Note that 'line_iterator()' isn't something that (as far as I know) is defined in the C++ standard library, but there's nothing preventing it.
Compare the code you wrote with what I wrote.

Lazy evaluation gets you all that, with no extra code, all the time.

Imagine writing that iterator stuff for every single applicable thing in the entire program. There are also other places where lazy evaluation is useful and you can't use iterators, like a function that will only use a portion of its arguments depending on some condition.

> But in a language with mutating side effects, and iterators, I'm not sure what this buys you...

Side-effect free, functional style of programming.

Edward Kmett's answer to this[1] question on Quora does a good job of explaining the value of lazy evaluation.

[1] http://www.quora.com/Programming-Languages/When-did-you-real...

I would also like to add that laziness adds a lot of power and flexibility to common procedures.

For example, let's look at a canonical quicksort in Haskell:

  quicksort :: Ord a => [a] -> [a]

  quicksort []     = []

  quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)

      where

          lesser  = filter (< p) xs

          greater = filter (>= p) xs
This is nothing too interesting, but now let's say you want to write a function to find the minimum value of a list

In Python, this would probably be done like this:

  def min_list(lst):

      current_minimum = lst[0]

      for e in lst[1:]:

          if e < current_minimum:

              current_minimum = e

      return current_minimum
However, in Haskell, a very similar procedure can be defined like this:

  minimum xs = head (quicksort xs)


(head returns the first element of a list)

Now, if have the slightest care for efficiency, you would never, ever write a function like this in a strict language.

However, in Haskell, minimum [5,2,7,1,3] would be evaluated like this:

  > minimum [5,2,7,1,3]

  > head (quicksort [5,2,7,1,3])
Now, head 'demands' one element from quicksort, so quicksort would start getting evaluated

  > head (lesser ++ ...)  [p = 5]
(++) is nonstrict, and only evaluates the elements you really need. Since right now only one element is demanded, we can expect that nothing to the right of (++) would be evaluated

  > head ((quicksort (filter (<5) [2,7,1,3]) ) ++ ...)
Now filter is also lazy, and it will only evaluate elements as needed. Right now, only one element is demanded by quicksort(notice the (p:xs) in the definition. quicksort only cares about the 'p' right now)

  > head ((quicksort (2:(filter (<5) [7,1,3])) ++ ...)

  > head ((quicksort (filter (<2) 

                              xs) ++ ... ) ++ ...)                                     [p = 2, xs = filter (<5) [7,1,3]]

Now, quicksort demands another element from filter (<2) ..., so filter (<5) runs until it finds the first element that is also (<2) > head ((quicksort (1:(filter (<2) (filter (<5) [3] ++ ...)) ) ++ ...) ++ ...)

Filter will run through the entire list, comparing each element to 5,2 and 1 in order, and find nothing, returning [], which will hit the base case for quicksort []= []

  > head ( [] ++ [1] ++ ...)

Simplifying to

  > head ([1] ++ ...)
(++) has enough evaluated elements to give '1' to head, which finally returns

  > 1

Notice that we passed through the list only once, just like in the python, in contrast to running an entire quicksort. While this is not as efficient as the python version as it keeps a few unnessary elements in memory, and compares elements to all previously found minimums, it is much more efficient than evaluating quicksort, and is pretty damn cool. With some GHC magic, the memory usage is also reduced somewhat.

I personally believe that the expressiveness gained through this procedure(we wrote quicksort and gained minimum for free!) offsets the loss in efficiency.

|| and && operators in C-like languages are "short-circuited", i.e. (true || predicate) just returns (true) without evaluating (predicate). This is a form of lazy evaluation.