[Haskell-cafe] how to break foldl' ?
Henning Thielemann
lemming at henning-thielemann.de
Fri Sep 30 06:52:58 EDT 2005
On Fri, 30 Sep 2005, gary ng wrote:
> Hi,
>
> say if I want to sum a list of numbers but only until
> it hits a max limit.
>
> Currently, I control it through the function and
> basically do nothing when the max is hit. However, if
> the list is very long, would this mean the same
> function would be called for the rest of the list
> which can be a waste of cycle ?
Depends on how you implemented it. If your implementation ignores the
result of the rest of the list, then it won't be computed.
This should work as expected:
takeWhile (<maxX) (scanl (+) 0 xs)
More information about the Haskell-Cafe
mailing list