简体   繁体   English

Haskell无法将类型'[Char]'与'Char'列表理解匹配

[英]Haskell Couldn't match type ‘[Char]’ with ‘Char’ list comprehension

So I'm trying to go through a list of films and output the title of each film, the compiler is saying that is couldn't match type [Char] with Char . 所以我试图通过一系列电影并输出每部电影的标题,编译器说这是不能匹配类型[Char]Char The thing is I want [Char] (A string) and not Char ?? 问题是我想要[Char] (一个字符串)而不是Char I'm confused as to why it wants a character and not a string? 我很困惑为什么它想要一个角色而不是一个字符串?

fas:: [Film] -> String
fas database = [title x | x <- database]

test.hs:55:27: error:
    • Couldn't match type ‘[Char]’ with ‘Char’
      Expected type: Char
        Actual type: String
    • In the expression: title x
      In the expression: [title x | x <- database]
      In an equation for ‘fas’:

          fas database = [title x | x <- database]

Any help would be greatly appreciated, I've probably worded this poorly which hasn't helped me when trying to search other people have the same problem :/ 任何帮助将不胜感激,我可能措辞这个很差,这在试图搜索其他人有同样问题时没有帮助我:/

regards, a novice 问候,新手

If I understand what you want to do correctly, the signature should be: 如果我理解你想要正确做什么,签名应该是:

fas:: [Film] -> [String]

Indeed, a list of strings . 的确, 一串字符串

Why? 为什么? Because you map every film to its title. 因为你将部电影都映射到它的标题。 Now since a title is a String , the result is a list of String s. 现在因为标题是一个String ,结果是一个String列表。

The compiler is actually complaining in the other way than you interpret it : since type String = [Char] , you wrote implicitly: 编译器实际上是以你解释它的方式抱怨 :因为type String = [Char] ,你隐式写了:

fas :: [Film] -> [Char] -- incorrect

whereas it should be: 而它应该是:

fas :: [Film] -> [[Char]]

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

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