How to get normal value from IO action in Haskell

Viewed 15991

I have the following function:

get :: Chars -> IO Chars
get cs = do
    char <- getChar
    let (dats, idx) = (curData cs, curIndex cs)
    let (x,y:xs) = splitAt idx dats
    let replacement = x ++ (ord char) : xs
    return $ Chars replacement idx

and I'd like to get a Chars value out of it, not an IO action. I have no idea how to do this, or if it is even possible.

The Chars type is basically just a container:

data Chars = Chars {
               curData  :: [Int],
               curIndex :: Int
               -- etc.
             }

The specifics aren't that important, I just want to know if there's a way for this function to return a Chars instead of an IO Chars.

If not, how do I pass this as an argument to a function that takes a Chars? I'm kind of new to Haskell I/O, but I don't think I want all of my functions that take Chars as arguments to instead have to take IO Chars as arguments, and then extract and repackage them. It seems unnecessary.

Thanks!

2 Answers
Related