4 ms·
What about these? f1 list = foldr (&&) True list -- && is logical and f2 list = foldr (||) False list -- || is logical or f3 list = foldl m
by nandemo 15y ago
What about these?
f1 list = foldr (&&) True list -- && is logical and
f2 list = foldr (||) False list -- || is logical or
f3 list = foldl max (head list) (tail list)
f4 list = foldl min (head list) (tail list)
Do you consider them trivial, or too hard to understand without rewriting as a loop? Honest question.
I think if you use a non-associative function as an argument, then fold is hard to understand. But then an explicit loop won't help much.
- tikhonj 15y agoCoincidentally, I've always found finding the maximum or minimum of a list with a for loop really finicky. It's much easier with a fold.
- reinhardt 15y agoTrivial, they're in the same category with add and multiply. Check out http://lambda-the-ultimate.org/node/587 http://lambda-the-ultimate.org/node/587 for some non trivial examples.