[Haskell-cafe] Help mixing pure and IO code

klondike klondikehaskellcafe at xiscosoft.es
Sun Nov 29 09:57:28 EST 2009

papa.eric at free.fr escribió:
> Hi haskell helpers,
> Learning haskell, I wanted to explore how to write pure code and then
> add some IO on top of it, keeping the main code pure. The idea was to
> write a very simple two-player game, then define some strategies to
> play it that do not involve IO, and finally use strategies involving
> Random or IO ("ask the user"). I failed to reuse the pure code, and
> the only solution I found was to rewrite most things for IO. Here is
> my attempt in literate haskell, it is quite short, I hope someone will
> be kind enough to tell me what I have missed... Thanks for any answer!
I know mainly two options, the first is using unsafe IO which is a small

The other is using the Control.Monad lift function which will apply pure
functions over IO ones returning an IOized type.

Ie if we have an
  b :: IO Int
  b = return 3
and we do
  c = lift (+ 2) b
We will get IO(5) when we use c

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 262 bytes
Desc: OpenPGP digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20091129/9e2daf08/signature.bin

More information about the Haskell-Cafe mailing list