我有以下代码,摘自 Hutton 的《Haskell 编程》,第二版,第 12 页:
seqn [] = return []
seqn (act:acts) = do x <- act
xs <- seqn acts
return (x::xs)
为此,ghci 告诉我
seqn.hs:4:30: error:
• Couldn't match expected type ‘xs’ with actual type ‘a’
‘xs’ is a rigid type variable bound by
an expression type signature:
forall xs. xs
at seqn.hs:4:33-34
‘a’ is a rigid type variable bound by
the inferred type of seqn :: Monad m => [m a] -> m [a1]
at seqn.hs:(1,1)-(4,35)
• In the first argument of ‘return’, namely ‘(x :: xs)’
In a stmt of a 'do' block: return (x :: xs)
In the expression:
do x <- act
xs <- seqn acts
return (x :: xs)
• Relevant bindings include
x :: a (bound at seqn.hs:2:22)
acts :: [m a] (bound at seqn.hs:2:11)
act :: m a (bound at seqn.hs:2:7)
seqn :: [m a] -> m [a1] (bound at seqn.hs:1:1)
|
4 | return (x::xs)
| ^
Failed, no modules loaded.
我在这里缺少什么?
这可能是课程中或您自己的拼写错误。应该是:
seqn [] = return []
seqn (act : acts) = do
x <- act
xs <- seqn acts
return (x : xs)
我们可以将其简化为:
seqn [] = return []
seqn (act : acts) = do
x <- act
(x :) <$> seqn acts