Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: How to convert from IO (a) to a? [duplicate]

Tags:

haskell

I have a function that returns me IO (Map.Map String Double) and I need Map.Map String Double, without IO.

I tried to do this, but it fails with: Not in scope: data constructor `IO'.

extractIO (IO (a)) = a
like image 993
Alin Ciocan Avatar asked Dec 08 '22 15:12

Alin Ciocan


2 Answers

You can't just unwrap IO, that's the entire point of IO.

If you have an IO (Map.Map String Double) and you want to process that thing, you have to do it within monadic context, i.e.

stuff :: IO ()
stuff = do
  map <- theThingThatReturnsYourIOMap
  theThingThatNeedsYourUnwrappedMap map

If you explain in more detail what you want to do, we can give you a more detailed answer.

like image 152
Sebastian Redl Avatar answered Dec 11 '22 05:12

Sebastian Redl


http://cvs.haskell.org/Hugs/pages/libraries/base/System-IO-Unsafe.html

unsafePerformIO

But it's a bad idea.

like image 42
wheybags Avatar answered Dec 11 '22 04:12

wheybags