自己实现的forM,我找不到错误
own implementation of forM, I can't find an error
你能告诉我我哪里错了吗?我从 monads 开始,我真的不明白错误。似乎有效
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
你能告诉我我哪里错了吗?我从 monads 开始,我真的不明白错误。似乎有效
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