附加的是文本csv数据库文件,其中包含名为Student.txt的学生信息。该信息包括名字,中间名首字母,姓氏,电话号码,电子邮件和gpa。您必须编写以下F#程序:
在添加带有电话号码,电子邮件地址和4.0 gpa的Malachi Constant之后,您应该找到以下内容:
答案 0 :(得分:0)
尽管缺少编写完整程序的关键信息,但大多数要求仅需要几行代码。让我们开始为CSV文件中的行建模,然后为第2点到第5点编写一些基本代码:
type Phone = Phone of string
type Email = Email of string
type StudentInfo =
{ firstName : string;
middleInitial : char option;
lastName : string;
phone : Phone;
email : Email option;
gpa : float }
// Three functions below left incomplete due to missing
// information, and/or as an exercise to the reader
let createPhone input = […]
let createEmail input = […]
let readStudentsFromCSV filename = […]
let students = readStudentsFromCSV "Students.txt"
// Print how many students have a 3.0 gpa or higher
students
|> List.filter (fun s -> s.gpa >= 3.0)
|> List.length
|> printfn "%d students have a GPA of 3.0 or higher."
// Print name (first, middle initial, last) and gpa
// of all students with the last name Anderson
students
|> List.filter (fun s -> s.lastName = "Anderson")
|> List.iter (fun s ->
printfn "%s %s%s — GPA: %f"
s.firstName
(match s.middleInitial with
| None -> ""
| Some c -> string c + " ")
s.lastName
s.gpa)
// Print how many students do not have an email account
students
|> List.filter (fun s -> Option.isNone s.email)
|> List.length
|> printfn "%d students do not have an email account."
// Print the average gpa of all students
students
|> List.averageBy (fun s -> s.gpa)
|> printfn "Average GPA of all students: %f."