What is the IO type in Haskell

Oto-Obong Eshiett

I am new to the Haskell programming language, I keep on stumbling on the IO type either as a function parameter or a return type.

playGame :: Screen -> IO ()

OR

gameRunner :: IO String -> (String -> IO ()) -> Screen -> IO ()

How does this work, I am a bit confused because I know a String expects words and an Int expects numbers. Whats does the IO used in functions expect or Return?

chi

Briefly put:

f1 :: A -> B -> C

is a function which takes two arguments of type A and B and returns a C. It does not perform any IO.

f2 :: A -> B -> IO C

is similar to f1, but can also perform IO.

f3 :: (A -> B) -> IO C

takes as an argument a function A -> B (which does not perform IO) and produces a C, possibly performing IO.

f4 :: (A -> IO B) -> IO C

takes as an argument a function A -> IO B (which can perform IO) and produces a C, possibly performing IO.

f5 :: A -> IO B -> IO C

takes as an argument a value of type A, an IO action of type IO B, and returns a value of type C, possibly performing IO (e.g. by running the IO action argument one or more times).

Example:

f6 :: IO Int -> IO Int
f6 action = do
   x1 <- action
   x2 <- action
   putStrLn "hello!"
   x3 <- action
   return (x1+x2+x3)

When a function returns IO (), it returns no useful value, but can perform IO. Similar to, say, returning void in C or Java. Your

gameRunner :: IO String -> (String -> IO ()) -> Screen -> IO ()

function can be called with the following arguments:

arg1 :: IO String
arg1 = do
   putStrLn "hello"
   s <- readLine
   return ("here: " ++ s)

arg2 :: String -> IO ()
arg2 str = do
   putStrLn "hello"
   putStrLn str
   putStrLn "hello again"

arg3 :: Screen
arg3 = ... -- I don't know what's a Screen in your context

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Haskell-(Type declaration) what is "a"?

What is the type of (1 2) in Haskell?

What is the meaning of parentheses in Haskell type signatures?

What does `Num a => a` mean in Haskell type system?

What is the purpose of including the type in its definition in haskell?

Haskell: What does type f a actually mean?

What is RealFloat type in Haskell used for?

what is the difference between type "a" and type "t" in Haskell type signature?

What does `... -> t a ->...` mean in type signature in Haskell?

What is the type of foldr map in haskell?

What is the default type evaluation of MonadPlus in Haskell?

What makes two type expressions in Haskell equivalent?

What does type signature for `undefined` mean in Haskell?

What is the type of map.map in Haskell?

Haskell: What does it mean for a type signature to be total?

What is the type of <- in Haskell?

What is the type of Haskell's bind function?

What is the type of Nothing in Haskell?

Haskell function type not what expected

"Couldn't match type `Maybe' with `IO' Expected type: IO String Actual type: Maybe String" In Haskell

Haskell Couldn't match type `[]' with `IO'

Haskell Expected type: IO Char Actual type: String

what is the type () :: () in haskell means?

Haskell - Couldn't match type [] with IO

What's wrong with my Haskell type synonym?

Haskell: Error: Couldn't match type ‘[]’ with ‘IO’

What is the type keyword in Haskell

Haskell IO: Couldn't match type `IO' with `[]'

What does the symbol `!` mean in type declarations in Haskell?