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?
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.
Comments