在Haskell中执行多个命令的基本问题

时间:2018-12-14 22:01:03

标签: haskell haskell-stack haskell-platform

我的代码中包含以下内容:

  where
    launch :: MonadIO m => m (Maybe Text)
    launch = do
        line <- launchLine
        return $ lineToText <$> line

    launchLine :: MonadIO m => m (Maybe Line)
    launchLine = fold (inproc "juke" ["launch", "--profile", "jukeplay", pack baseImage] mempty) $
        Fold selectLaunchName Nothing id

上面的行工作正常。我的问题(和问题)是我想在此启动行之前执行另一个命令,所以可能是这样的:

  where
    launch :: MonadIO m => m (Maybe Text)
    launch = do
        line <- launchLine
        return $ lineToText <$> line

    launchLine :: MonadIO m => m (Maybe Line)
    launchLine = fold (inproc "juke" ["image", "copy", "jukebox:" <> pack baseImage, "local:", "--copy-aliases"] mempty) $
        Fold selectLaunchName Nothing id

    launchLine :: MonadIO m => m (Maybe Line)
    launchLine = fold (inproc "juke" ["launch", "--profile", "jukeplay", pack baseImage] mempty) $
        Fold selectLaunchName Nothing id

这显然行不通。我该怎么办?

我需要在执行“ juke启动..”之前完成此“ juke映像副本”

预先感谢您的帮助

1 个答案:

答案 0 :(得分:4)

您可以将单子动作与(>>)组合在一起:

(>>) :: Monad m => m a -> m b -> m b

或使用do语法,将其简化为(>>)

\act1 act2 -> do {act1; act2} :: Monad m => m a -> m b -> m b

例如:

launch :: MonadIO m => m (Maybe Text)
launch = do
    preLaunchLine
    line <- launchLine
    return $ lineToText <$> line

preLaunchLine :: MonadIO m => m (Maybe Line)
preLaunchLine = fold (inproc "juke" ["image", "copy", "jukebox:" <> pack baseImage, "local:", "--copy-aliases"] mempty) $
    Fold selectLaunchName Nothing id

launchLine :: MonadIO m => m (Maybe Line)
launchLine = fold (inproc "juke" ["launch", "--profile", "jukeplay", pack baseImage] mempty) $
    Fold selectLaunchName Nothing id