Skip to content

Instantly share code, notes, and snippets.

@lukaskollmer
Created March 18, 2019 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaskollmer/0d73a44159ca129df4b11d570268d3a4 to your computer and use it in GitHub Desktop.
Save lukaskollmer/0d73a44159ca129df4b11d570268d3a4 to your computer and use it in GitHub Desktop.
An OCaml program to calculate your TUM CS GPA
#!/usr/bin/env ocaml
(*
gpa.ml
An OCaml program to calculate your TUM CS GPA
Made by Lukas Kollmer (lukas.kollmer@gmail.com)
*)
(* grades is a two-dimensional a list, containing all grades sorted by semester *)
(* Note that if a course is half-weighed, you have to asjust the ects (see info1 in the example below) *)
type grade = float * float * string (* ects, value, course *)
let grades: grade list list = [
[ (* 1. semester*)
(6., 1.0, "pgdp");
(3., 2.4, "info1");
...
];
[ (* 2. semester*)
(8., 1.7, "era praktikum");
(6., 1.7, "eist");
...
];
...
]
let () =
let rec calc_sem (x, y) = function
| [] -> x, y
| (e, v, _) :: grades ->
calc_sem (x +. e *. v, y +. e) grades
in
Printf.printf "GPA after semester:\n" ;
List.fold_left (fun acc sem ->
calc_sem (List.hd acc) sem :: acc
) [0., 0.] grades
|> List.rev |> List.tl
|> List.iteri (fun i (x, y) -> Printf.printf "%i.: %f\n" (i+1) (x/.y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment