[Haskell-beginners] Unmaybe

Kim-Ee Yeoh ky3 at atamo.com
Tue May 14 14:08:09 CEST 2013


On Tue, May 14, 2013 at 4:21 PM, Adrian May
<adrian.alexander.may at gmail.com>wrote:

> I have a really annoying scrap of code:
>
> unmaybe Nothing = mempty
> unmaybe (Just dia) = dia
>
> It happened because I'm using Diagrams but building my diagram requires
> looking something up in a list using findIndex, which returns Maybe Int.
>

What instance of Monoid is this? Because Int has both a Sum Int and a
Product Int instance so you can't just apply unmaybe to (Just 3 :: Maybe
Int).

Defining unmaybe Nothing = 0 prompts the question: how will you distinguish
misses versus hits on the head of the list? Presumably you don't want to.

You might be interested in the totalized lookup functions defined in my
private toolkit (hayoo returns nothing):

-- tlookup :: (Eq a) => b -> a -> [(a, b)] -> b
tlookup b a abs = fromMaybe b $ lookup a abs
tlookup0  a abs = tlookup mempty a abs

-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130514/8e7fb6f1/attachment.htm>


More information about the Beginners mailing list