Personal tools

How to get rid of IO

From HaskellWiki

Revision as of 09:08, 28 April 2009 by Lemming (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


1 Question

I have something of type
IO a
, but I need something of type

How can I get that?

2 Answer

You can get rid of it, but we don't tell you, how, since it is certainly not what need. It is the special safety belt of Haskell, that you cannot get rid of IO! Nonetheless, the biggest parts of Haskell programs are and should be non-IO functions. This is achieved by plugging together IO functions and non-IO functions

using atomic combinator functions like

If that scares you, you can hide that in the do notation, which will then look quite conveniently as:

do text <- readFile "foo"
   writeFile "bar" (someComplicatedNonIOOperation text)

Btw. using the combinators this would look like

writeFile "bar" . someComplicatedNonIOOperation =<< readFile "foo"

2.1 What we didn't tell you at the beginning

Btw. The function that answers your initial question is
. It is however not intended for conveniently getting rid of the

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 the
part of the name.

(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