haskell - 自己实现的forM,找不到错误

标签 haskell compiler-errors monads

你能告诉我我哪里错了吗?我从单子(monad)开始,我真的不明白错误。它似乎正在工作

myforM :: (Monad m) => [a] -> (a -> m b) -> m [b]
myforM [] _ = return []
myforM (x:xs) f = (f x) >>= (\r -> myforM xs >>= (\rs -> return (r:rs)))

第一个错误:
 m.hs:110:36:
        Couldn't match type ‘m’ with ‘(->) (a -> [b0])’
          ‘m’ is a rigid type variable bound by
              the type signature for
                myforM :: Monad m => [a] -> (a -> m b) -> m [b]
              at monady1.hs:108:11
        Expected type: m [b]
          Actual type: (a -> [b0]) -> [[b0]]
        Relevant bindings include
          f :: a -> m b (bound at monady1.hs:110:15)
          xs :: [a] (bound at monady1.hs:110:11)
          x :: a (bound at monady1.hs:110:9)
          myforM :: [a] -> (a -> m b) -> m [b] (bound at monady1.hs:109:1)
        Possible cause: ‘myforM’ is applied to too many arguments
        In the first argument of ‘(>>=)’, namely ‘myforM xs’
        In the expression: myforM xs >>= (\ rs -> return (r : rs))

第二个错误:
   m.hs:110:36:
            Couldn't match type ‘b’ with ‘[b0]’
              ‘b’ is a rigid type variable bound by
                  the type signature for
                    myforM :: Monad m => [a] -> (a -> m b) -> m [b]
                  at monady1.hs:108:11
            Expected type: m [b]
              Actual type: (a -> [b0]) -> [[b0]]
            Relevant bindings include
              r :: b (bound at monady1.hs:110:31)
              f :: a -> m b (bound at monady1.hs:110:15)
              myforM :: [a] -> (a -> m b) -> m [b] (bound at monady1.hs:109:1)
            Possible cause: ‘myforM’ is applied to too many arguments
            In the first argument of ‘(>>=)’, namely ‘myforM xs’
            In the expression: myforM xs >>= (\ rs -> return (r : rs))
        Failed, modules loaded: none.

最佳答案

myforM xs >>= ...

类型为 (a -> m b) -> m [b] .你错过了f这里。该错误有点误导:它包含问题,但不是正确的原因:
Expected type: m [b]
Actual type: (a -> [b0]) -> [[b0]]
...
Possible cause: ‘myforM’ is applied to too many arguments

关于haskell - 自己实现的forM,找不到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36371683/

相关文章:

java - 编译 : tools. jar 未找到 fatal error :

macos - ghc 动态库中的_closure 和_info 符号

haskell - 违反适用仿函数法

haskell - 函数的仿函数/应用程序/单子(monad)实例的用例

perl - libapreq2 : libtool: link: CURRENT `' must be a nonnegative integer

function - Monad 和函数之间的区别

database - 软件事务内存是否与数据库事务相同?

Scala 使用 () 应用 PartialFunction 与 .apply() 不同

scala - 使用 Scala-cats 隐式解析

c# - Bind 和 Map 之外的 Option<T> monad 的标准操作