OCaml : How to get a parameter from a type? -
this simple can't find answer anywhere.
let's created new type student = string * int tuple of student's name , score on test.
then list of student passed function, , have find average score of class (or list).
let blah (class : student list) : float = match class [] -> [] | hd :: tl -> hd ?????? so start standard pattern matching, 1 element of list (aka student) how extract test score? feel it's obvious , elementary i've coded ints, strings, int lists, int list list, etc.. hd piece of information want.
you need more elaborate pattern:
match class | [] -> ... | (name, score) :: tl -> ... in cases can use fst , snd components of pair, matching gives cleaner code (in experience).
Comments
Post a Comment