Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Haskell IO list to list type [duplicate]

Tags:

haskell

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?

like image 813
123Ex Avatar asked Feb 17 '26 11:02

123Ex


1 Answers

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
like image 137
HaskellElephant Avatar answered Feb 19 '26 12:02

HaskellElephant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!