Possible Duplicate:
haskell-problem: io string -> [int]
How I can convert Haskell IO list to normal list?
IO [value] -> [value]
Is there any built in function to do this?
There are ways of doing what you ask for, but it is unsafe. Therefore, I think you should be looking at the problem the other way around. In staid of getting the list out of IO, you should lift your pure function into IO.
Let's say you wanted to get the list from DB and apply some pure function to it, then you could do the following:
yourFunc = do
list <- getListFromDB
return (myPureFunction List)
or if you want to print the result afterwords:
yourFunc = do
list <- getListFromDB
print (myPureFunction List)
In general, in order to calculate a pure result inside of the IO monad you can use let:
yourFunc = do
list <- getListFromDB
let result = myPureFunction list
return result
or more compact:
import Control.Monad((=<<))
import Control.Applicative((<$>))
yourFunc = print =<< myPureFunction <$> getListFromDB
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With