0

是否可以检查给定字符串是否代表 $PATH(%path%) 中的可执行文件?它必须可移植到 Windows。仅调用它并查看返回状态的想法是不合适的,因为非零可能意味着程序错误或未找到程序。好吧,在代码中,我想关注

possibleCompilers = ["gcc", "icc", "foo", "bar.exe"]
presentCompiler :: IO String
4

1 回答 1

7

该任务应该可以使用System.Directory.findExecutable.

possibleCompiler :: IO (Maybe String)
possibleCompiler = check possibleCompilers
  where
    check [] = return Nothing
    check (c:cs) = do
       mbc <- findExecutable c
       case mbc of
         Just _ -> return mbc
         Nothing -> check cs

我将类型更改为IO (Maybe String),因为可能找不到任何候选人。

于 2012-07-13T09:52:54.007 回答