[Haskell-beginners] how to skip pattern match error when applying a mapM_

PICCA Frederic-Emmanuel frederic-emmanuel.picca at synchrotron-soleil.fr
Tue Jan 17 14:03:02 UTC 2017


Sorry I forgot to put a subject
________________________________________
De : Beginners [beginners-bounces at haskell.org] de la part de PICCA Frederic-Emmanuel
Envoyé : mardi 17 janvier 2017 11:15
À : The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell
Objet : [Haskell-beginners] (no subject)

Hello,

Here a reduction of my problem

values :: IO [IO (Maybe Int)]
values = do
  let v = [Just 1, Just 2, Just 3, Nothing, Just 5, Nothing, Just 7] :: [Maybe Int]
  return $ map return v

main :: IO ()
main = do
  vs <- values
  nvs <- mapM_ go vs
  print nvs
    where
      go :: IO (Maybe Int) -> IO Int
      go v' = do
         Just v <- v'
         return v

when I run this script, I get a runtime error

picca at diffabs6:~/tmp$ runhaskell test.hs
test.hs: user error (Pattern match failure in do expression at test.hs:13:10-15)

What I want is a go method which skip silently the (IO Nothing) values.
so when used in the mapM_ it return only the values which are returned by the IO (Maybe Int) (stored in the values)

Thanks for your help

Frédéric


Indeed
_______________________________________________
Beginners mailing list
Beginners at haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


More information about the Beginners mailing list