Polo
Polo

Reputation: 113

Writting to file without returning an IO() element

Would it be possible to write in a file without returning an IO() element in the function. For the moment, I am only able to write to a file by returning IO() in my function f.

f:: Type -> IO()
f sequent = do 
    --let result = ...
    let file = "tmp/log.txt"
    writeToFile file ("TIMES rule: " ++  (show result))


writeToFile :: FilePath -> String -> IO()
writeToFile file content = do 
    x <- SIO.readFile file
    writeFile file ("\n"++content)
    appendFile file x

Would it be possible to have something as follows instead,

f:: Type -> String
f sequent = do 
    --let result = ...
    let file = "tmp/log.txt"
    -- do without returning
    writeToFile file ("TIMES rule: " ++  (show result))
    -- return the result
    result

Upvotes: 1

Views: 114

Answers (1)

Matthias Berndt
Matthias Berndt

Reputation: 4587

This is intentional. The idea behind IO is that you can see from the type of the function whether it will perform side effects.

Upvotes: 4

Related Questions