[Haskell-beginners] (no subject)

Magnus Therning magnus at therning.org
Tue Jan 24 08:58:44 UTC 2017

PICCA Frederic-Emmanuel <frederic-emmanuel.picca at synchrotron-soleil.fr> writes:

> 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]

I would let the compiler deal with the type, i.e. skip the ` :: [Maybe Int]`.

>   return $ map return v

Wrapping the list *and* its items in `IO` seems excessive. Would it not
be easier to have `values :: IO [Maybe Int]`?

> 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)

You can't, not with the route you've taken here. It would be the
equivalent of skipping the `else` clause of an `if` statement. I suggest
you rethink your approach of how to remove (filter out) the `Nothing`
items in the list.


Magnus Therning              OpenPGP: 0x927912051716CE39
email: magnus at therning.org   jabber: magnus at therning.org
twitter: magthe               http://therning.org/magnus

Increasingly, people seem to misinterpret complexity as sophistication,
which is baffling—the incomprehensible should cause suspicion rather than
     — Niklaus Wirth
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170124/5a42472a/attachment.sig>

More information about the Beginners mailing list