[Haskell-beginners] Organizing cmd line flow
John Wiegley
johnw at fpcomplete.com
Wed Jan 30 22:41:22 CET 2013
>>>>> Bryan Vicknair <bryanvick at gmail.com> writes:
> Is there any way to get closer to the following? I think this is much
> clearer, but perhaps I'm missing a much larger point in how this should be
> done in Haskell::
> createDb fpath = do
> checkFileExists fpath
> checkParentDirExists fpath
> con <- openCon (Config fpath)
> create con
> closeCon con
Here is one way:
{-# LANGUAGE DeriveDataTypeable #-}
import Control.Applicative
import Control.Exception
import Control.Monad
import Control.Monad.Trans.Error
import Data.Data
import Data.Typeable
import System.Directory
data ConfirmationFailed = ConfirmationFailed String
deriving (Show, Data, Typeable)
instance Exception ConfirmationFailed
confirm desc test = do
result <- test
unless result $ throwIO (ConfirmationFailed desc)
main = do
confirm "/tmp exists" (doesDirectoryExist "/tmp")
print "hello"
confirm "/tmpx exists" (doesDirectoryExist "/tmpx")
print "hello"
You can also use "assert" or "guard" for this, although neither will be as
descriptive.
--
John Wiegley
FP Complete Haskell tools, training and consulting
http://fpcomplete.com johnw on #haskell/irc.freenode.net
More information about the Beginners
mailing list