如何使用 >>=
过滤 IO [Maybe String]
,只保留列表中的 Just
值并保持 IO 上下文。
-- returns Just, if the passed binary-name is not present on the system
binDoesntExist :: String -> IO (Maybe String)
binDoesntExist ...
我的当前解决方案没有使用绑定运算符:
missingBin :: [String] -> IO [String]
missingBin xs = do
ys <- mapM (\x -> binDoesntExist x) xs
return $ catMaybes ys
我目前正在学习 Haskell 并尝试理解如何使用标准库中的不同函数。我的解决方案可行,但我想有更简洁的方式。
binDoesn'tExist
,它依然可以工作。 - Daniel Wagner