COMP 3031 Assignment 1 SML programming solved

$35.00

Category: You will receive a download link of the .ZIP file upon Payment

Description

5/5 - (1 vote)

Question 1. Inserting courses
Write a function insert_course that returns a course with a given list of (cid, pid)’s
inserted into the given course C L. If a cid already exists in the course ids in L, the function
skips the tuple; otherwise, the tuple is appended to the end of L, in the order they appear in
the given input tuple list.
val insert_course = fn : (string * string) list * course -> course
Examples:
– insert_course ([], C [(“comp10”, “p01”)]);
val it = C [(“comp10″,”p01”)] : course
– insert_course ([(“comp12”, “p02”)], C []);
val it = C [(“comp12″,”p02”)] : course
– insert_course ([(“comp10”, “p02”)], C [(“comp10”, “p01”)]);
val it = C [(“comp10″,”p01”)] : course
– insert_course ([(“comp13”, “p01”), (“comp12”, “p02”)], C [(“comp10”,
“p01”)]);
val it = C [(“comp10″,”p01”),(“comp13″,”p01”),(“comp12″,”p02”)] :
course
Question 2. Inserting a student’s enrollment
Write a function insert_enroll that returns an enroll variable with a given tuple (sid,
course_list) inserted, where sid is the student id, and course_list is a list of distinct
course ids. If the student id exists in the enroll, append the course ids in the given
course list to the end of the course list of the student in the enroll, such that all course ids
3
of the student are still distinct and the newly inserted course ids are in the same order as
in the input list; otherwise, append the given tuple to the end of the enrollment list.
val insert_enroll = fn : (int * string list) * enroll -> enroll
Examples:
– insert_enroll ((1702, []), E [(1701, [“comp10”, “comp11”])]);
val it = E [(1701,[“comp10″,”comp11”]),(1702,[])] : enroll
– insert_enroll ((1701, [“comp10”, “comp11”]), E []);
val it = E [(1701,[“comp10″,”comp11”])] : enroll
– insert_enroll ((1701, [“comp11”, “comp10”]), E [(1701, [“comp10”,
“comp12”])]);
val it = E [(1701,[“comp10″,”comp12″,”comp11”])] : enroll
– insert_enroll ((1702, [“comp10”]), E [(1701, [“comp10”, “comp11”])]);
val it = E [(1701,[“comp10″,”comp11”]),(1702,[“comp10”])] : enroll
Question 3. Listing the students enrolled in a course
Write a function query_students that returns a list of ids of all the students enrolled in a
course with the given course id. The order of student ids in the returned list is the same as
the order they appear in the enroll.
val query_students = fn : string * enroll -> int list
Examples:
– query_students (“comp10”, E []);
val it = [] : int list
– query_students (“comp10”, E [(1701, [“comp10”])]);
val it = [1701] : int list
– query_students (“comp11”, E [(1701, [“comp10”])]);
val it = [] : int list
– query_students (“comp10”, E [(1701, [“comp10”, “comp11”]), (1702,
[“comp13”, “comp10”]), (1703, [])]);
val it = [1701,1702] : int list
Question 4. Counting the number of distinct students enrolled in a
professor’s courses
Write a function count_distinct_students that returns the number of distinct students
enrolled in all the courses taught by the professor pid.
4
val count_distinct_students = fn : string * course * enroll -> int
Examples:
– count_distinct_students (“p01”, C [], E []);
val it = 0 : int
– count_distinct_students (“p01”, C [(“comp10”, “p01”)], E []);
val it = 0 : int
– count_distinct_students (“p01”, C [(“comp10”, “p01”)], E [(1701,
[“comp10”]), (1702, [“comp13”])]);
val it = 1 : int
– count_distinct_students (“p01”, C [(“comp10”, “p01”), (“comp12”,
“p02”), (“comp13”, “p01”)], E [(1701, [“comp10”, “comp11”]), (1702,
[“comp13”])]);
val it = 2 : int
Question 5. Deleting a course
Write a function delete_course_enroll that deletes a course of a given course id and its
corresponding enrollments. This function returns a tuple consisting of an updated course
and an updated enroll. The order of the tuples in the output remains the same as the order
before the deletion.
val delete_course_enroll = fn : string * course * enroll -> course *
enroll
Examples:
– delete_course_enroll (“comp10”, C [], E[]);
val it = (C [],E []) : course * enroll
– delete_course_enroll (“comp10”, C [(“comp10”, “p01”)], E []);
val it = (C [],E []) : course * enroll
– delete_course_enroll (“comp10”, C [(“comp10”, “p01”)], E [(1701,
[“comp10”])]);
val it = (C [],E [(1701,[])]) : course * enroll
– delete_course_enroll (“comp10”, C [(“comp10”, “p01”), (“comp12”,
“p02”)], E [(1701, [“comp10”, “comp11”]), (1702, [“comp13”, “comp10”]),
(1703, [])]);
val it =
(C[(“comp12″,”p02”)],E [(1701,[“comp11”]),(1702,[“comp13”]),(1703,[])])
: course * enroll