例如:
newfile :: FilePath -> IO Bool
newfile x | length x <= 0 = return False
| doesFileExist x == True = return False
| otherwise = return True
这可以工作吗?
最佳答案
您已经在 IO
monad,那么为什么不使用以下内容呢?
newfile :: FilePath -> IO Bool
newfile x | length x <= 0 = return False
| otherwise = do exists <- doesFileExist x
return $ not exists
对于应用性的好处:
import Control.Applicative
newfile :: FilePath -> IO Bool
newfile x | length x <= 0 = return False
| otherwise = not <$> doesFileExist x
如您所见,应用路径比您想在问题中使用的守卫更简洁!
关于haskell - 在 Haskell 中,有没有办法在函数保护中进行 IO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2138819/