【发布时间】:2015-09-26 00:19:55
【问题描述】:
我有一个返回类型IO Bool 的函数。我想将此函数用作filterM 的参数,但我真正想做的是反转它的输出。我已经尝试了(not . f) 的效果,但not 并不符合IO 的氛围。如何反转IO Bool?
这是一个最小的工作示例:
#!/usr/bin/env runhaskell
{-# LANGUAGE UnicodeSyntax #-}
module Main where
import Prelude.Unicode
userEnteredStr ∷ String → IO Bool
userEnteredStr str = do
input ← getLine
return (input ≡ str)
-- doesn't work. How would I write this function?
--userDidntEnterStr ∷ String → IO Bool
--userDidntEnterStr str = not . userEnteredStr
main = do result ← userEnteredStr "y"
print result
对不起,如果这是基本的!我在 Hoogle 上找不到类型为 IO Bool -> IO Bool 的函数,也没有在我的网络搜索中找到任何内容。
【问题讨论】:
-
尝试使用
(Bool -> Bool) -> (IO Bool -> IO Bool)代替。它最终得到了正确的答案……我承认,它在列表中很低,但它确实得到了它。(a -> b) -> (IO a -> IO b)排在首位。
标签: haskell boolean monads functor