How to get rid of IO
From HaskellWiki
(Difference between revisions)
(Small text about that really FAQ) |
m (→Answer: copy editing: fix whitespace, remove acronyms, remove value judgments, correct grammar) |
||
| (2 intermediate revisions not shown.) | |||
| Line 6: | Line 6: | ||
== Answer == | == Answer == | ||
| - | You can get rid of it, but | + | You can get rid of it, but you almost certainly don't need to. The special safety belt of Haskell is that you cannot get rid of IO! Nonetheless, the biggest parts of Haskell programs are and should be non-IO functions. Applications using both IO and non-IO functions are written by plugging together these two flavors of functions using functions like <hask>(>>=)</hask>. These functions allow useful IO while having all the safety properties of a pure [[functional programming]] language. |
| - | + | ||
| - | Nonetheless, the biggest parts of Haskell programs are and should be non-IO functions. | + | You can hide some functions using [[do notation]], which looks like this: |
| - | + | ||
| - | using | + | |
| - | + | ||
| - | which | + | |
<haskell> | <haskell> | ||
do text <- readFile "foo" | do text <- readFile "foo" | ||
writeFile "bar" (someComplicatedNonIOOperation text) | writeFile "bar" (someComplicatedNonIOOperation text) | ||
</haskell> | </haskell> | ||
| - | + | Without hiding the functions, this would look like: | |
<haskell> | <haskell> | ||
writeFile "bar" . someComplicatedNonIOOperation =<< readFile "foo" | writeFile "bar" . someComplicatedNonIOOperation =<< readFile "foo" | ||
| Line 24: | Line 20: | ||
=== What we didn't tell you at the beginning === | === What we didn't tell you at the beginning === | ||
| - | + | There is a function which directly answers the initial question, namely, <hask>unsafePerformIO</hask>. | |
It is however not intended for conveniently getting rid of the <hask>IO</hask> constructor. | It is however not intended for conveniently getting rid of the <hask>IO</hask> constructor. | ||
It must only be used to wrap IO functions that behave like non-IO functions, | It must only be used to wrap IO functions that behave like non-IO functions, | ||
| Line 33: | Line 29: | ||
== See also == | == See also == | ||
| + | * [[Introduction to IO]] | ||
* [[Avoiding IO]] - Avoiding IO in the first place is a good thing, and we tell you how to achieve that | * [[Avoiding IO]] - Avoiding IO in the first place is a good thing, and we tell you how to achieve that | ||
| + | * [[Tutorials#Practical_Haskell|Tackling the awkward squad]] | ||
* http://www.haskell.org/wikisnapshot/ThatAnnoyingIoType.html | * http://www.haskell.org/wikisnapshot/ThatAnnoyingIoType.html | ||
* http://www.haskell.org/wikisnapshot/UsingIo.html | * http://www.haskell.org/wikisnapshot/UsingIo.html | ||
[[Category:FAQ]] | [[Category:FAQ]] | ||
| + | [[Category:Monad]] | ||
Current revision
Contents |
1 Question
I have something of typeIO a
a
How can I get that?
2 Answer
You can get rid of it, but you almost certainly don't need to. The special safety belt of Haskell is that you cannot get rid of IO! Nonetheless, the biggest parts of Haskell programs are and should be non-IO functions. Applications using both IO and non-IO functions are written by plugging together these two flavors of functions using functions like(>>=)
You can hide some functions using do notation, which looks like this:
do text <- readFile "foo" writeFile "bar" (someComplicatedNonIOOperation text)
Without hiding the functions, this would look like:
writeFile "bar" . someComplicatedNonIOOperation =<< readFile "foo"
2.1 What we didn't tell you at the beginning
There is a function which directly answers the initial question, namely,unsafePerformIO
IO
It must only be used to wrap IO functions that behave like non-IO functions,
Since this property cannot be checked by the compiler, it is your task and thus theunsafe
(Some library writers have abused that name component for partial functions. Don't get confused!) You will only need this in rare cases and only experienced programmers shall do this.
3 See also
- Introduction to IO
- Avoiding IO - Avoiding IO in the first place is a good thing, and we tell you how to achieve that
- Tackling the awkward squad
- http://www.haskell.org/wikisnapshot/ThatAnnoyingIoType.html
- http://www.haskell.org/wikisnapshot/UsingIo.html
Categories: FAQ | Monad
