It looks quite neat to use the Maybe monoid here:
import Data.Monoid
searchList p = foldr (\x - if p x then mappend (Just [x]) else id) Nothing
but it seems that the Maybe Monoid instance keeps this strict. I
fiddled with this a bit, and came up with the following:
instance (Monoid m) =
On Wed, Nov 18, 2009 at 7:43 AM, Ben Millwood hask...@benmachine.co.ukwrote:
It looks quite neat to use the Maybe monoid here:
import Data.Monoid
searchList p = foldr (\x - if p x then mappend (Just [x]) else id)
Nothing
but it seems that the Maybe Monoid instance keeps this strict. I
Using the same basic structure you did, and foldr, I think below is the
simplest method:
import Data.Maybe
searchList :: (a - Bool) - [a] - Maybe [a]
searchList p xs = foldr (\x acc - if p x then Just (x: fromMaybe [] acc)
else acc) Nothing xs
ghci
On Tue, Nov 17, 2009 at 6:31 PM, Ezra Lalonde ezra.lalo...@gmail.com wrote:
Using the same basic structure you did, and foldr, I think below is the
simplest method:
import Data.Maybe
searchList :: (a - Bool) - [a] - Maybe [a]
searchList p xs = foldr (\x acc - if p x
On Tue, Nov 17, 2009 at 7:39 PM, David Menendez d...@zednenem.com wrote:
On Tue, Nov 17, 2009 at 6:31 PM, Ezra Lalonde ezra.lalo...@gmail.com wrote:
Using the same basic structure you did, and foldr, I think below is the
simplest method:
import Data.Maybe
searchList
On Tue, Nov 17, 2009 at 10:01 PM, Luke Palmer lrpal...@gmail.com wrote:
filter even [0..] -- [0,2,4,6,8,...]
searchList even [0...] -- Just [0,2,4,6,8,...]
searchList gives Nothing in exactly those cases that filter gives [].
They give _|_ in exactly the same situations. searchList