2 ms·
An example use of bracket would be: do filename <- getLine fileContents <- bracket (openFile filename) (closeFile) $ \file -> do conte
by lalaithion 7y ago
An example use of bracket would be:
do
filename <- getLine
fileContents <- bracket (openFile filename) (closeFile) $ \file -> do
contents <- readFile file
if contents == ""
then do
writeFile file "hello world"
return "hello world"
else do
return contents
putStrLn fileContents
this ensures that the file is closed even if readFile or writeFile throw exceptions.