我正在为这样的排序功能编写一个记录器:
bubble :: (Ord a) => [a] -> Writer [String] [a]
bubble (x:y:ys)
| x > y = do
tell [show x ++ " why does this not work"]
y:bubble(x:ys)
| otherwise = do
tell [show y ++ " is a number"]
x:bubble(y:ys)
bubble [x] = do
tell ["nothing works"]
return [x]
但我收到此错误:
Couldn't match expected type `WriterT
[String] Data.Functor.Identity.Identity [a]'
with actual type `[a0]'
In a stmt of a 'do' block: y : bubble (x : ys)
In the expression:
do { tell [show x ++ " why does this not work"];
y : bubble (x : ys) }
In an equation for `bubble':
bubble (x : y : ys)
| x > y
= do { tell [show x ++ " why does this not work"];
y : bubble (x : ys) }
| otherwise
= do { tell [show y ++ " is a number"];
x : bubble (y : ys) }
Failed, modules loaded: none.
我已逐字阅读此错误消息,但我没有更接近问题所在?我尝试在没有减速的情况下进行编译,出现如下一组新错误:
q.hs:21:17:
No instance for (MonadWriter [[Char]] [])
arising from a use of `tell'
Possible fix:
add an instance declaration for (MonadWriter [[Char]] [])
In a stmt of a 'do' block:
tell [show x ++ " why does this not work"]
In the expression:
do { tell [show x ++ " why does this not work"];
y : bubble (x : ys) }
In an equation for `bubble':
bubble (x : y : ys)
| x > y
= do { tell [show x ++ " why does this not work"];
y : bubble (x : ys) }
| otherwise
= do { tell [show y ++ " is a number"];
x : bubble (y : ys) }
Failed, modules loaded: none.