【发布时间】:2019-07-21 12:16:56
【问题描述】:
基本上我有这个练习:回忆上周的 StudentMark 类型同义词。写一个递归函数:
listMarks :: String -> [StudentMark] -> [Int]
给出特定学生的分数列表;例如:
listMarks "Joe" [("Joe", 45), ("Sam", 70), ("Joe", 52)] = [45,52]
这是我编写函数的方式:
type StudentMark = (String, Int)
listMarks :: String -> [StudentMark] -> [Int]
listMarks _ [] = []
listMarks std (x:xs)
| std == fst x = snd x : listMarks (fst x) xs
| otherwise = listMarks (fst x) xs
如果列表中的字符串与“std”字符串不同,这将不起作用。我想了解为什么以及如何进行这项工作?谢谢!
【问题讨论】:
标签: list haskell recursion tuples