[Haskell-cafe] List instance of Alternative: why (++)?
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
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
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>
> 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:
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe