Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (**
- Student modules using include module
- *)
- type grade = Pass | No_pass
- let string_of_grade = function
- | Some Pass -> "Pass"
- | Some No_pass -> "No pass"
- | None -> "Not graded"
- (**
- "Base-class"
- *)
- module Student = struct
- type student = {
- name : string;
- tests : int list;
- course_grade : grade option;
- }
- let make name = {
- name = name;
- tests = [];
- course_grade = None
- }
- let to_string s =
- Printf.sprintf "Student: %s; number of tests made: %d; course grade: %s"
- s.name
- (List.length s.tests)
- (string_of_grade s.course_grade)
- end
- module GraduateStudent = struct
- include Student
- let compute_course_grade s =
- let tests = s.tests in
- let total = List.fold_left (+) 0 tests in
- let grade = match total / List.length tests with
- | q when q >= 80 -> Pass (* <-- 80 for grad *)
- | _ -> No_pass
- in
- {s with course_grade = Some grade}
- end
- module UndergraduateStudent = struct
- include Student
- let compute_course_grade s =
- let tests = s.tests in
- let total = List.fold_left (+) 0 tests in
- let grade = match total / List.length tests with
- | q when q >= 70 -> Pass (* <-- 70 for undergrad *)
- | _ -> No_pass
- in
- {s with course_grade = Some grade}
- end
- type grad_students =
- | Grad of GraduateStudent.student
- | Undergrad of UndergraduateStudent.student
- let _ =
- let student1 = GraduateStudent.make "Foo Barsson" in
- let student2 = UndergraduateStudent.make "J.R.R Tolkien" in
- let roster = [
- Grad student1;
- Undergrad student2;
- ] in
- List.iter (fun s ->
- print_endline (match s with
- | Grad s -> UndergraduateStudent.to_string s (* <-- Don't want this to compile *)
- | Undergrad s -> UndergraduateStudent.to_string s
- )
- ) roster;
- ()
Advertisement
Add Comment
Please, Sign In to add comment