Je vous écris un enregistreur pour une fonction de tri comme ceci:Haskell Monad.Writer
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]
mais je reçois cette erreur:
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.
J'ai lu ce message d'erreur mot pour mot, mais je je ne suis pas plus proche de quel est le problème? J'ai essayé de compiler avec la décélération, pour une nouvelle série d'erreurs comme ceci:
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.
hmm est 1. Ord a 2. une fonction appelée avec une liste 3. l'opérande pour ajouter un élément à une liste? Dois-je changer mes déclarations? –
Non, votre déclaration me semble bonne. plus :) – mergeconflict
@baronaron Votre déclaration de type est correcte, le problème est dans la ligne 'y: bubble (x: ys)'. Voir mon edit – dave4420