我是初学者,任何帮助都将受到赞赏。我的目标是能够从任何给定的电影中提取评级作为 Float ,以便我可以正确操作数据,例如给出“平均评级”膜。
type Title = String
type Director = String
type Year = Int
type Mark = Int
type Rating = (String, Float)
-- Define Film type here
type Film = (Title, Director, Year, [Rating])
典型的电影看起来像
("True Lies", "James Cameron", 1994, [("Dave",3), ("Kevin",10), ("Jo",0)])
我试过了
extractRating :: Film -> [(String, Float)]
extractRating (_, _, _, rating) = rating
然后调用这个函数
putStrLn (extractRating "True Lies")
如果它可以帮助我这里帮助我错误转储
haskell.hs:82:21:
Couldn't match type ‘(String, Float)’ with ‘Char’
Expected type: String
Actual type: [(String, Float)]
In the first argument of ‘putStrLn’, namely
‘(extractRating "True Lies")’
In a stmt of a 'do' block: putStrLn (extractRating "True Lies")
haskell.hs:82:35:
Couldn't match type ‘[Char]’
with ‘(Title, Director, Year, [Rating])’
Expected type: Film
Actual type: [Char]
In the first argument of ‘extractRating’, namely ‘"True Lies"’
In the first argument of ‘putStrLn’, namely
‘(extractRating "True Lies")’
In a stmt of a 'do' block: putStrLn (extractRating "True Lies")
答案 0 :(得分:1)
我不能发表评论,但我强烈建议调查数据关键字和模式匹配,而不是使用元组。
PS:编辑更有帮助。无论如何,我看到的问题是“真实的谎言”。 extractRating的输入是Film类型,当你给它一个字符串时。解决这个问题的方法是将输入变为电影,只需将其更改为字符串,然后根据给定字符串查找任何匹配项以返回评级。