Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert [IO Int] to IO [Int] in Haskell?

Tags:

haskell

monads

I have this code that fits my need:

f :: [IO Int] -> IO [Int]
f [] = return []
f (x:xs) = do 
  a <- x
  as <- f xs
  return (a:as)

But I thougth there would be a predefined way (msum ?)

But I can't see how.

Any help would be welcome. Thx

like image 472
asbxl Avatar asked Apr 05 '13 11:04

asbxl


People also ask

How do I get int from IO Int Haskell?

There is no way to get the "Int" out of an "IO Int", except to do something else in the IO Monad. The first argument is your initial "IO Int" value that "comboBoxGetActive" is returning. The second is a function that takes the Int value and turns it into some other IO value.

What does IO mean in Haskell?

Haskell separates pure functions from computations where side effects must be considered by encoding those side effects as values of a particular type. Specifically, a value of type (IO a) is an action, which if executed would produce a value of type a .

What does the type IO Int signify?

The IO type serves as a tag for operations (actions) that interact with the outside world. The IO type is abstract: no constructors are visible to the user.


Video Answer


1 Answers

Yes, it's available in the standard library under the name sequence. It has a more general type than your f: Monad m => [m a] -> m [a], since it works for any Monad, not just IO.

You could find it yourself by searching for type [IO a] -> IO [a] on Hoogle.

like image 187
Mikhail Glushenkov Avatar answered Sep 19 '22 21:09

Mikhail Glushenkov