How to convert IO int to String in Haskell?

核能气质少年 提交于 2020-01-06 19:36:50

问题


I'm learning to use input and output in Haskell. I'm trying to generate a random number and output it to another file. The problem is that the random number seems to be returning "IO int" that I can't covert it to String using "show". Could someone give me a pointer here??


回答1:


It's helpful if you show us the code you've written that isn't working.

Anyway, you are in a do block and have written something like this, yes?

main = do
    ...
    writeFile "some-file.txt" (show generateRandomNumberSomehow)
    ...

You should instead do something like this:

main = do
    ...
    randomNumber <- generateRandomNumberSomehow
    writeFile "some-file.txt" (show randomNumber)
    ...

The <- operator binds the result of the IO Int value on the right to the Int-valued variable on the left. (Yes, you can also use this to bind the result of an IO String value to a String-valued variable, etc.)

This syntax is only valid inside a do block. It's important to note that the do block will itself result in an IO value --- you can't launder away the IO-ness.




回答2:


dave4420's answer is what you want here. It uses the fact that IO is a Monad; that's why you can use the do notation.

However, I think it's worth mentioning that the concept of "applying a function to a value that's not 'open', but inside some wrapper" is actually more general than IO and more general than monads. It's what we have the Functor class for.

For any functor f (this could, for instance, be Maybe or [] or IO), when you have some value
wrapped :: f t (for instance wrapped :: Maybe Int), you can use fmap to apply a function
t -> t' to it (like show :: Int -> String) and get a
wrappedApplied :: f t' (like wrappedApplied :: Maybe String).

In your example, it would be

genRandomNumAsString :: IO String
genRandomNumAsString = fmap show genRandomNumPlain


来源:https://stackoverflow.com/questions/12892814/how-to-convert-io-int-to-string-in-haskell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!