top | item 22645483

(no title)

EvilTerran | 6 years ago

> if you're reducing the list to a scalar, you lose all the benefits this article wants to claim.

Not quite - for example, these both benefit from foldr, despite each producing a scalar:

  and, or :: [Bool] -> Bool
  and = foldr (&&) True
  or = foldr (||) False
... as they can both "bail out early" without evaluating the entire list (if they find a False or a True, respectively).

The distinction lies not so much in "are you reducing to a scalar?" as "can your binary operation be productive without evaluating its second parameter?" - or, if you prefer, "is it ever lazy in its second parameter?". If so, then foldr may be appropriate.

discuss

order

No comments yet.