我是Haskell编程的新手,我有一个存储学生数据和考试标记的数据库,如下所示
type Student = String
type MarkProject = Float
type MarkMidSem = Float
type MarkFinal = Float
type Database = [(Student, MarkProject, MarkMidSem, MarkFinal)]
exampleBase :: Database
exampleBase = [("Barry", 23, 24, 43),
("Wayne", 22, 20, 49),
("Ivan", 21, 25, 48),
("Alli", 24, 24, 48),
("Martial", 22, 25, 49)]
我想创建一个功能来查找学生的考试总分。
TotalMarks = MarkProject + MarkMidSem + MarkFinal
我曾多次尝试过他的功能但失败了。我还设置了一个函数来插入student,markProject,markmidsem和markfinal,如:
totalMark :: Student -> MarkProject -> MarkMidSem -> MarkFinal -> Float
有人可以指导我如何完成totalMarks的功能吗?提前谢谢。
答案 0 :(得分:3)
我会为数据元组创建一个额外的类型,并使数据库使用该类型,之后只需创建一个获取元组的函数,模式匹配它并总结你需要的内容。
type Student = String
type MarkProject = Float
type MarkMidSem = Float
type MarkFinal = Float
type StudentData = (Student, MarkProject, MarkMidSem, MarkFinal)
type Database = [StudentData]
exampleBase :: Database
exampleBase = [("Barry", 23, 24, 43),
("Wayne", 22, 20, 49),
("Ivan", 21, 25, 48),
("Alli", 24, 24, 48),
("Martial", 22, 25, 49)]
totalMark::StudentData -> Float
totalMark (n, mp, mms, mf) = mp + mms + mf