[Haskell-beginners] a simple little problem

Mihai Maruseac mihai.maruseac at gmail.com
Sat Aug 13 20:11:11 CEST 2011


On Sat, Aug 13, 2011 at 9:04 PM, 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)

I'd go by:

fn l = let l' = filter (/= Nothing) l in if l' == [] then Nothing else head l'

-- 
Mihai



More information about the Beginners mailing list