haskell - 在 Haskell 中,有没有办法在函数保护中进行 IO?

标签 haskell io guard

例如:

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/

相关文章:

performance - Hadoop MapReduce 为多个作业读取一次数据集

ruby-on-rails - rspec/guard 运行 minitest 并获取无效选项 : -f

rspec - 如何创建仅运行已更改的测试文件的 Guard 配置监视?

java - 将 MP3 播放列表保存到文件

haskell - "The type signature for ‘main’ 缺少伴随绑定(bind)是什么意思”?

Haskell(haskell)单词补全

haskell - 链完整的概念是什么?

haskell - 从 createProcess 外部获取的句柄读取

c - C 中的作用域守卫

haskell - 关于 Haskell 中的组合的困惑