Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
IO inside
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Example: a list of I/O actions === Let's try defining a list of I/O actions: <haskell> ioActions :: [IO ()] ioActions = [(print "Hello!"), (putStr "just kidding"), (getChar >> return ()) ] </haskell> I used additional parentheses around each action, although they aren't really required. If you still can't believe that these actions won't be executed immediately, just recall the simplifed type of this list: <haskell> ioActions :: [RealWorld -> ((), RealWorld)] </haskell> Well, now we want to execute some of these actions. No problem, just insert them into the <code>main</code> chain: <haskell> main = do head ioActions ioActions !! 1 last ioActions </haskell> Looks strange, right? Really, any I/O action that you write in a <code>do</code>-expression (or use as a parameter for the <code>(>>)</code>/<code>(>>=)</code> operators) is an expression returning a result of type <span style="white-space: nowrap"><code>IO a</code></span> for some type <code>a</code>. Typically, you use some function that has the type <span style="white-space: nowrap"><code>x -> y -> ... -> IO a</code></span> and provide all the <code>x</code>, <code>y</code>, etc. parameters. But you're not limited to this standard scenario - don't forget that Haskell is a functional language and you're free to compute the functional value required (recall that <span style="white-space: nowrap"><code>IO a</code></span> is really a function type) in any possible way. Here we just extracted several functions from the list - no problem. This functional value can also be constructed on-the-fly, as we've done in the previous example - that's also OK. Want to see this functional value passed as a parameter? Just look at the definition of <code>when</code>. Hey, we can buy, sell, and rent these I/O actions just like we can with any other functional values! For example, let's define a function that executes all the I/O actions in the list: <haskell> sequence_ :: [IO a] -> IO () sequence_ [] = return () sequence_ (x:xs) = do x sequence_ xs </haskell> No mirrors or smoke - we just extract I/O actions from the list and insert them into a chain of I/O operations that should be performed one after another (in the same order that they occurred in the list) to "compute the final world value" of the entire <code>sequence_</code> call. With the help of <code>sequence_</code>, we can rewrite our last <code>main</code> action as: <haskell> main = sequence_ ioActions </haskell> Haskell's ability to work with I/O actions as with any other (functional and non-functional) values allows us to define control structures of arbitrary complexity. Try, for example, to define a control structure that repeats an action until it returns the <code>False</code> result: <haskell> while :: IO Bool -> IO () while action = ??? </haskell> Most programming languages don't allow you to define control structures at all, and those that do often require you to use a macro-expansion system. In Haskell, control structures are just trivial functions anyone can write.
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width