
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 have something like import Data.Maybe fn list = case catMaybes list of [] -> Nothing [x:_] -> fromJust x Next, an augmentation of this idea (or similar idea). fn2 :: [Maybe Float] -> Map Int Float When a Just x appears at position n in the list, then put (key=n, value=x) into the map. I have: import qualified Data.Map as M f2 list = M.fromList [(n,x) | (n,Just x) <- zip [0..] list]

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

Mihai Maruseac
I'd go by:
fn l = let l' = filter (/= Nothing) l in if l' == [] then Nothing else head l'
If you want to disregard the useful Monoid instance of Maybe, then here is a nicer way to express this: fn :: [Maybe a] -> Maybe a fn = listToMaybe . catMaybes Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

On Sat, Aug 13, 2011 at 9:23 PM, Ertugrul Soeylemez
Mihai Maruseac
wrote: I'd go by:
fn l = let l' = filter (/= Nothing) l in if l' == [] then Nothing else head l'
If you want to disregard the useful Monoid instance of Maybe, then here is a nicer way to express this:
fn :: [Maybe a] -> Maybe a fn = listToMaybe . catMaybes
I stand corrected. I only wanted a simple solution but the Monoid instance is more expressive :)

Dennis Raddle
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/

On Saturday 13 August 2011, 20:04:57, Dennis Raddle 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)
import Control.Monad fn = msum (or, with only imports from Data.Maybe: listToMaybe . catMaybes)
I have something like
import Data.Maybe fn list = case catMaybes list of [] -> Nothing [x:_] -> fromJust x
Next, an augmentation of this idea (or similar idea).
fn2 :: [Maybe Float] -> Map Int Float
When a Just x appears at position n in the list, then put (key=n, value=x) into the map.
I have:
import qualified Data.Map as M f2 list = M.fromList [(n,x) | (n,Just x) <- zip [0..] list]
That's fine.

On Sat, Aug 13, 2011 at 14:04, Dennis Raddle
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)
Isn't this just mconcat? -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms

On Sat, Aug 13, 2011 at 02:47:04PM -0400, Brandon Allbery wrote:
On Sat, Aug 13, 2011 at 14:04, Dennis Raddle
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)
Isn't this just mconcat?
No. The Monoid instance for Maybe defined in Data.Monoid is the one which lifts a Monoid instance on a to a Monoid instance on Maybe a. That is, all the Nothings are discarded and all the Justs combined according to the Monoid instance on a. For example:
mconcat [Nothing, Just "hello", Nothing, Just "world"] Just "helloworld"
The MonadPlus instance for Maybe is what the OP wants (as I think someone else already pointed out), which is sensible, since the Maybe monad models possible failure, and hence the MonadPlus instance models choosing the first success. -Brent

On Sun, Aug 14, 2011 at 12:36, Brent Yorgey
On Sat, Aug 13, 2011 at 02:47:04PM -0400, Brandon Allbery wrote:
Isn't this just mconcat?
No. The Monoid instance for Maybe defined in Data.Monoid is the one which lifts a Monoid instance on a to a Monoid instance on Maybe a.
Yeh, as usual I realized that about 5 minutes after sending, by which time someone else had already pointed it out. -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms
participants (6)
-
Brandon Allbery
-
Brent Yorgey
-
Daniel Fischer
-
Dennis Raddle
-
Ertugrul Soeylemez
-
Mihai Maruseac