【问题标题】:Haskell : Couldn't match type ‘Char’ with ‘[Char]’ Expected type: [String] Actual type: StringHaskell:无法将“Char”类型与“[Char]”匹配预期类型:[String] 实际类型:String
【发布时间】:2018-06-02 13:53:17
【问题描述】:

我有这个代码:

censName :: String -> String
censName [] = []
censName xs = unwords(censWords(words xs))

censWords :: [String] -> [String]
censWords [] = []
censWords (x:xs) = if isUpper(head x) then replace(tail x) else censWords xs 

replace :: String -> String
replace [] = []
replace (x:xs) = '*':replace xs

错误提示:

Couldn't match type ‘Char’ with ‘[Char]’
  Expected type: [String]
    Actual type: String

我的问题是:为什么!?我的意思是,在 replace(tail x) x 中,x 是一个字符串,那么为什么它需要 [String]?

在此处发布的类似问题上,我找不到适合我的问题的任何内容。

【问题讨论】:

    标签: haskell


    【解决方案1】:
    censWords :: [String] -> [String]
                          -- ^^^^^^^^ expected
    censWords (x:xs) = if .. then replace(tail x) else ..
                               -- ^^^^^^^^^^^^^^^ actual value
    

    返回值是单个字符串,但应该是字符串列表。

    问题不在于对replace 的调用,而是它返回的值与预期的类型不同。

    【讨论】:

      猜你喜欢
      • 2015-06-08
      • 2014-08-30
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多