haskell

时间:2015-10-27 11:33:41

标签: haskell

在定义函数的代码中,有一个奇怪的“模式匹配”(cradleRootDir -> projdir)

我想这意味着应用内联函数并将结果绑定到名称projdir

该构造的名称是什么?

withGhcModEnv' :: (IOish m, GmOut m) => (FilePath -> (Cradle -> m a) -> m a) -> FilePath -> Options -> ((GhcModEnv, GhcModLog) -> m a) -> m a
withGhcModEnv' withCradle dir opts f =
    withCradle dir $ \crdl ->
      withCradleRootDir crdl $
        f (GhcModEnv opts crdl, undefined)
 where
   withCradleRootDir (cradleRootDir -> projdir) a = do
       cdir <- liftIO $ getCurrentDirectory
       eq <- liftIO $ pathsEqual projdir cdir
       if not eq
          then throw $ GMEWrongWorkingDirectory projdir cdir
          else a

构造函数

data Cradle = Cradle {
    cradleCurrentDir :: FilePath
  , cradleRootDir    :: FilePath
  , cradleCabalFile  :: Maybe FilePath
  , cradlePkgDbStack  :: [GhcPkgDb]
  } deriving (Eq, Show)

1 个答案:

答案 0 :(得分:7)

使用View Patterns

  

评估要将值v与模式匹配(expr - &gt; pat),   评估(expr v)并将结果与​​pat。匹配。

请参阅cabal file

Default-Extensions:   ScopedTypeVariables, RecordWildCards, NamedFieldPuns,
                      ConstraintKinds, FlexibleContexts,
                      DataKinds, KindSignatures, TypeOperators, ViewPatterns
                                                                ^^^^^^^^^^^^
                                                                |  |  |  | |