I want a program that takes a text and changes some words, in particular it should write "Leonardo" every time it reads "Chiara" and vice-versa. This is my code:
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
However, when I try to run it I get this error:
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.
What is going wrong? I am completly new to Haskell and I have tried to look for answers to similar problems but it is really, really hard for me to understand what's going on.
A string in Haskell is a list of Char; Haskell denotes its type as [Char]
.
If you write 'C'
, then Haskell will interpret this as the Char C
.
If you write "C"
, then Haskell will interpret this as a single-character string "C".
And Haskell is pedantic about using the right type.
So to give you the more specific solution:
The problem originates in the first part.
changeText (x:xs)
| x == "C" = isChiara x xs
| x == "L" = isLeo x xs
| otherwise = x ++ changeText xs
The x
is a Char, but you compare it to "C"
or "L"
- which Haskell interprets as a string of 1 character.
Make it:
changeText (x:xs)
| x == 'C' = isChiara x xs
| x == 'L' = isLeo x xs
| otherwise = x ++ changeText xs
And the error message should be gone.
Since you already solved this, let me show a reasonably short 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.