[Haskell-cafe] Short-circuiting a fold

David House dmhouse at gmail.com
Thu Apr 5 14:19:17 EDT 2007

On 05/04/07, Kurt Hutchinson <kelanslists at gmail.com> wrote:
> Straightforward:
> > ssfold p f z = head . dropWhile ( not . p ) . scanl f z

I'd prefer find instead of head . dropWhile (not . p), making the
result type a Maybe, as this is essentially equivalent to searching
through the result of a scan for a specific value, so it should follow
find's example and use Maybe for its partiality.

> I want to see some real creativity here, so pull out all the stops.

You may also be interested in the Compose experiment [1]: a bit of fun
seeing how many different ways compose :: [a -> a] ->(a -> a) could be
defined. Most are quite silly, but the solution involving the State
monad, for example, is really quite elegant.

[1]: http://haskell.org/haskellwiki/Compose

-David House, dmhouse at gmail.com

More information about the Haskell-Cafe mailing list