繁体   English   中英

Haskell:无法将类型`[Char]'与`Char'匹配

[英]Haskell: Couldn't match type `[Char]' with `Char'

我想要一个带文本并改变一些单词的程序,特别是它应该在每次读取“Chiara”时写“Leonardo”,反之亦然。 这是我的代码:

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

但是,当我尝试运行它时,我收到此错误:

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.

出了什么问题? 我完全是Haskell的新手,我试图寻找类似问题的答案,但我真的很难理解发生了什么。

Haskell中的字符串是Char的列表; Haskell将其类型表示为[Char]

如果你写'C' ,那么Haskell会把它解释为Char C
如果你写"C" ,那么Haskell会将其解释为单字符串“C”。
Haskell对于使用正确的类型很迂腐。


那么为您提供更具体的解决方案:

问题起源于第一部分。

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

x是一个Char,但你将它与"C""L" - Haskell将其解释为1个字符的字符串。

做了:

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

并且错误消息应该消失。

既然你已经解决了这个问题,那么让我展示一个相当短的选择

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM