[Haskell-cafe] All-fail case in asum

David Feuer david.feuer at gmail.com
Fri Jun 10 15:32:45 UTC 2016


Or, to keep it a fold,

asum xs = foldr go (fromMaybe empty) xs Nothing where
  go x r Nothing = r (Just x)
  go x r (Just prev) = prev <|> r (Just x)
On Jun 10, 2016 11:09 AM, "Dan Burton" <danburton.email at gmail.com> wrote:

Add a pattern match for the single-element list to stop it from hitting the
empty list base case.

asum [] = empty
asum [x] = x
asum (x:xs) = x <|> asum xs


On Friday, June 10, 2016, Tom Ellis <
tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk> wrote:

> On Fri, Jun 10, 2016 at 05:37:41PM +0300, Юрий Сыровецкий (Yuriy
> Syrovetskiy) wrote:
> > It is so because in base library
> >
> >     asum = foldr (<|>) empty
> >
> > What if it was defined
> >
> >     asum [] = empty
> >     asum [x:xs] = x <|> asum xs
>
> Those are the same (assuming you mean (x:xs) instead of [x:xs]).
>
> Tom
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>


-- 
-- Dan Burton

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160610/647de569/attachment.html>


More information about the Haskell-Cafe mailing list