[Haskell-beginners] a simple little problem

Ertugrul Soeylemez es at ertes.de
Sat Aug 13 20:11:54 CEST 2011


Dennis Raddle <dennis.raddle at gmail.com> wrote:

> Can someone suggest an elegant way to write the following?
>
> fn :: [Maybe Float] -> Maybe Float
>
> in which, if the input list has all Nothing, then the result is
> Nothing if the input list has one or more Just x, then the result is
> Just x (in which the x is picked arbitrarily, could be the first one
> or last one)

Either of:

    fnFirst :: [Maybe a] -> Maybe a
    fnFirst = getFirst . mconcat . map First

    fnLast :: [Maybe a] -> Maybe a
    fnLast = getLast . mconcat . map Last


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/





More information about the Beginners mailing list