I am writing a logger for a sorting function like this:
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]
but i get this error:
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.
I have read this error message word for word but I am not any closer to what the problem is?
I tried compiling with out the deceleration, for a fresh set of errors like this:
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.
A nod in the right direction:
y?bubble(x:ys)?(:)?Answers:
(In these answers,
ais the sameaas inbubble :: (Ord a) => [a] -> Writer [String] [a].)y :: abubble(x:ys) :: Writer [String] [a]bubbleis a function,bubble(x:ys)is the result of applyingbubbletox:ys(:) :: b -> [b] -> [b]:is an operator; whatever type its first operand has, its second operand must have the type “list of whatever type the first operand has”, and the result has that same list type.Given that you have given
yandbubble(x:ys)as the operands to:, can you see now what the problem is?