(no subject)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

(no subject)

PICCA Frederic-Emmanuel
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@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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: (no subject)

Magnus Therning

PICCA Frederic-Emmanuel <[hidden email]> 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@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.

/M

--
Magnus Therning              OpenPGP: 0x927912051716CE39
email: [hidden email]   jabber: [hidden email]
twitter: magthe               http://therning.org/magnus

Increasingly, people seem to misinterpret complexity as sophistication,
which is baffling—the incomprehensible should cause suspicion rather than
admiration.
     — Niklaus Wirth

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

signature.asc (847 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: (no subject)

PICCA Frederic-Emmanuel
Hello thanks for your time.

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

It was just a reduced example of my real problem.
And I wanted to express the type for th ehuman of the mailing list :)

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

No because the Maybe Int comes from an IO.
But we solved the problem using the MaybeT

MaybeT IO MyDataType


So as you said, I rethink this with the help of the mailing list guyes.

Thansk a lot


Frederic
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Loading...