News for hackers. Stuff that matters.
Re:Use Haskell for shell scripting by danidiaz on Friday January 30, 2015 @09:58AM (#8972280)
> What's the idiom for chdir'ing to a subdirectory such that you pop back out again when you're done

This can be done with the "bracket" function, which works roughly like a context manager in Python:

  import Control.Exception
  import System.Directory

  withDirectory :: FilePath -> IO a -> IO a
  withDirectory path action = bracket (getCurrentDirectory <* setCurrentDirectory path) 
                                      setCurrentDirectory 
                                      (const action)

There are no comments on this comment yet.