[Haskell-cafe] List instance of Alternative: why (++)?

Jon Purdy evincarofautumn at gmail.com
Sat May 6 22:23:41 UTC 2017


I’ve wanted this before as well. Maybe we should throw a newtype at it?

newtype LeftBiased a = LeftBiased [a]
instance Alternative (LeftBiased a) where
  empty = []
  [] <|> b = b
  a <|> _ = a

newtype RightBiased a = RightBiased [a]
instance Alternative (RightBiased a) where
  empty = []
  a <|> [] = a
  _ <|> b = b

This could be generalised to work on any Foldable, actually, if that’s
desirable. And of course the bikeshed could be a different colour as for
the names.

It’s unfortunate that the instance for Maybe is already biased differently;
I wonder if this instance would be useful, or if it’s already available
somewhere?

newtype Unbiased a = Unbiased (Maybe a)
instance (Monoid m) => Alternative (Unbiased m) where
  empty = Nothing
  Just a <|> Just b = Just (a <> b)
  _ <|> Just b = Just b
  Just a <|> _ = Just a
  _ <|> _ = Nothing


On Fri, May 5, 2017 at 11:12 PM, Theodore Lief Gannon <tanuki at gmail.com>
wrote:

> Fiddling around, I found myself wanting:
>
> coalesce :: [a] -> [a] -> [a]
> -- or -- :: (Foldable t) => t a -> t a -> t a
> coalesce a b = if null a then b else a
>
> I expected this to be (<|>) (it is for Maybe!) but instead I find no
> canonical implementation of it anywhere, and what seems like a useless instance
> Alternative []. What's the rationale?
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170506/d67f2979/attachment.html>


More information about the Haskell-Cafe mailing list