4 ms·
Thanks for the pointer! I'm having trouble categorizing this one, tbh. (also its relationship to what dwohnitmok says on this HN discussion) Superficially, the
by gnoack 7y ago
Thanks for the pointer! I'm having trouble categorizing this one, tbh. (also its relationship to what dwohnitmok says on this HN discussion)
Superficially, the code on this Wikipedia page looks like the bracket pattern in similar to the `dynamic-unwind` low-level function and friends in Scheme and Lisp - but then again I realize the notion of cleanup probably only makes sense when mutable state exists :)
How does that look in practice? Is there a clever way to use the bracket operator together with Haskell's syntactic sugar for monads?
- lalaithion 7y agoAn 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.