我正在寻找不使用递归的解决方案。我有3个数据,例如:
names = ["Alice","Bob","Charlie","David"]
-- their grades:
grades :: [[Int]]
grades = [[3,2],[4,6],[2,3],[1,4]]
-- And the weights for a total note
weights :: [Float]
weights = [0.75,0.25]
这应该理解为:
Alice's grades: 3 and 2 -- The grade 3 weighted with 75% and the grade 2 with 25%
Bob's grades: 4 and 6 -- The grade 4 weighted with 75% and the grade 6 with 25%
Charlie's grades: 2 and 3 -- The grade 2 weighted with 75% and the grade 3 with 25%
David's grades: 1 and 4 -- The grade 1 weighted with 75% and the grade 4 with 25%
我编写了一个函数,用于为一个学生计算averageGrade
:
averageGrade :: [Float] -> [Int] -> Float
我必须根据这种类型完成一项功能:
allStudents :: [String] -> [[Int]] -> [Float] -> [(String,Float)]
此功能应为每个人的元组提供平均等级,例如:
[("Alice",2.75),("Bob",4.5),etc....]
计算由averageGrade (weights) ([Specific Grades])
进行,例如
averageGrade (weights) ([3,2])
如果我想为Alice计算它。
如何在不通过head
和tail
遍历列表的情况下进行遍历(因为不允许递归)?