问题
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 valuewrapped :: f t
(for instance wrapped :: Maybe Int
), you can use fmap
to apply a functiont -> t'
to it (like show :: Int -> String
) and get awrappedApplied :: 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