2017-03-23 3 views
2

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.

Antwort

4

Eine Zeichenfolge in Haskell ist eine Liste von Char; Haskell bezeichnet seinen Typ als [Char].

Wenn Sie 'C' schreiben, interpretiert Haskell dies als Char C.
Wenn Sie "C" schreiben, interpretiert Haskell dies als Zeichenfolge mit einem einzelnen Zeichen "C".
Und Haskell ist pedantisch über die Verwendung der richtigen Art.


So können Sie die spezifischere Lösung zu geben:

Das Problem im ersten Teil stammt.

changeText (x:xs) 
    | x == "C" = isChiara x xs 
    | x == "L" = isLeo x xs 
    | otherwise = x ++ changeText xs 

Die x ist ein Char, aber Sie vergleichen es mit "C" oder "L" - die Haskell als eine Kette von 1 Zeichen interpretiert.

Machen Sie es:

changeText (x:xs) 
    | x == 'C' = isChiara x xs 
    | x == 'L' = isLeo x xs 
    | otherwise = x ++ changeText xs 

und die Fehlermeldung verschwunden sein sollte.

+1

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? –

+0

@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. –

+0

Das ist eigentlich ein Tippfehler, es sollte 'changeText xs' sein – Nadni

3

Da Sie bereits diese gelöst, lassen Sie zeigen mir eine recht kurze alternative

changeText :: String -> String 
changeText [] = [] 
changeText ('C':'h':'i':'a':'r':'a':xs) = "Leonardo" ++ changeText xs 
changeText ('L':'e':'o':'n':'a':'r':'d':'o':xs) = "Chiara" ++ changeText xs 
changeText (x:xs) = x : changeText xs 
+2

Es kann mit 'Data.List.stripPrefix' und' PatternGuards' ('changeText xs | Just xs '<- stripPrefix" Chiara "xs =" Leonardo "++ changeText xs'') oder' ViewPatterns noch sauberer gemacht werden '(' changeText (stripPrefix "Chiara" -> Nur xs) = "Leonardo" ++ changeText xs'). –

Verwandte Themen