1

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 an IO Int, something that I can't convert to a String using show.

Could someone give me a pointer here?

1

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much for your help!! I've been spending a whole day trying to figure this out >_<! Thanks!!
5

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

1 Comment

Just what I wanted to add, yes. +1. fmap is really handy in IO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.