Ich möchte ein Programm, das einen Text nimmt und einige Wörter ändert, insbesondere sollte es "Leonardo" jedes Mal schreiben, wenn es "Chiara" liest und umgekehrt versa. Dies ist mein Code:Haskell: Konnte nicht den Typ `[Char] 'mit` Char'
changeText [] = []
changeText (x:xs)
| x == "C" = isChiara x xs
| x == "L" = isLeo x xs
| otherwise = x ++ changeText x
isChiara x xs
| nome == "hiara" = "Leonardo" ++ changeText (drop 5 xs)
| otherwise = x ++ changeText xs
where nome = take 5 xs
isLeo x xs
| nome == "eonardo" = "Chiara" ++ changeText (drop 7 xs)
| otherwise = x ++ changeText xs
where nome = take 7 xs
Allerdings, wenn ich versuche, es zu laufen ich diesen Fehler:
main.hs:10:1: error:
Couldn't match type `[Char]' with `Char'
Expected type: [Char] -> [Char]
Actual type: [[Char]] -> [Char]
main.hs:17:13: error:
* Couldn't match type `Char' with `[Char]'
Expected type: [[Char]]
Actual type: [Char]
* In the second argument of `(==)', namely `"hiara"'
In the expression: nome == "hiara"
In a stmt of a pattern guard for
an equation for `isChiara':
nome == "hiara"
main.hs:17:49: error:
* Couldn't match type `[Char]' with `Char'
Expected type: [Char]
Actual type: [[Char]]
* In the first argument of `changeText', namely `(drop 5 xs)'
In the second argument of `(++)', namely `changeText (drop 5 xs)'
In the expression: "Leonardo" ++ changeText (drop 5 xs)
main.hs:18:33: error:
* Couldn't match type `[Char]' with `Char'
Expected type: [Char]
Actual type: [[Char]]
* In the first argument of `changeText', namely `xs'
In the second argument of `(++)', namely `changeText xs'
In the expression: x ++ changeText xs
main.hs:22:13: error:
* Couldn't match type `Char' with `[Char]'
Expected type: [[Char]]
Actual type: [Char]
* In the second argument of `(==)', namely `"eonardo"'
In the expression: nome == "eonardo"
In a stmt of a pattern guard for
an equation for `isLeo':
nome == "eonardo"
main.hs:22:49: error:
* Couldn't match type `[Char]' with `Char'
Expected type: [Char]
Actual type: [[Char]]
* In the first argument of `changeText', namely `(drop 7 xs)'
In the second argument of `(++)', namely `changeText (drop 7 xs)'
In the expression: "Chiara" ++ changeText (drop 7 xs)
main.hs:23:33: error:
* Couldn't match type `[Char]' with `Char'
Expected type: [Char]
Actual type: [[Char]]
* In the first argument of `changeText', namely `xs'
In the second argument of `(++)', namely `changeText xs'
In the expression: x ++ changeText xs
Failed, modules loaded: none.
Was nicht in Ordnung ist? Ich bin völlig neu bei Haskell und habe versucht, nach Antworten auf ähnliche Probleme zu suchen, aber es ist wirklich, wirklich schwer für mich zu verstehen, was vor sich geht.
Als Anfänger in Haskell selbst, ist die letzte Zeile von 'changeText' richtig? Wo es heißt 'sonst = x ++ changeText x' Ich denke, es heißt 'changeText' mit einem char, aber' changeText' erwartet eine Zeichenkette. Habe ich das falsch verstanden? –
@MattJones Ich denke du hast Recht. Habe keinen Haskell-Compiler zur Hand, oder ich würde es versuchen. Es sollte wahrscheinlich 'x ++ changeText [x]' sein. –
Das ist eigentlich ein Tippfehler, es sollte 'changeText xs' sein – Nadni