I also see no reason you would need laziness for 5% of programming problems. That seems absurd given that the overwhelming majority of programmers have gotten along without it for 50 years.
I also see no reason you would need laziness for 5% of programming problems. That seems absurd given that the overwhelming majority of programmers have gotten along without it for 50 years.
For example in C/C++ your only ability to introduce laziness is via manual thunking, if, or the logical conjunctive (&&), and disjunction (||) which have non-strict semantics. For example you often see the idiom of:
if (x != NULL && x.foo == bar) { ... }
Which relies on (&&) being lazy in its second argument. Lazy languages allow one to write things like && and if without needing them baked in. For example: until :: Bool -> a -> a -> a
until b t f = case (not b)
True -> t
False -> fYou just answered your own question. "If you don't need to compute a value, then don't compute it" is laziness.
>That seems absurd given that the overwhelming majority of programmers have gotten along without it for 50 years.
No they haven't. Most programmers don't understand it, but they tend to use it on a regular basis.
No -- "laziness" is saying "the programmer wants us to compute this value, but we don't need it just yet, so we're going to create a thunk that will sit on the heap until it's evaluated or garbage collected". Strictness is doing what the programmer says, which involves the programmer deciding what does and does not have to be computed while he's writing the program.
For example, imagine something happens which invalidates a property which must be calculated (say, a bounding box for a graphical object).
Instead of immediately recomputing it, just set it to nil, and only recompute it the next time the property is actually accessed.
This way you can harmlessly invalidate it multiple times without doing a potentially expensive calculation (which just gets thrown out by subsequent invalidations).
if (logger.isDebugEnabled())
logger.debug("..." + ...);In other words, "sort lst" gives you a sorted list, but "take 5 (sort lst)" gives you the first five elements, without sorting the rest of the list.
return theObject; }