DEV Community

Discussion on: OCaml Speedrun! 🐫🐪

Collapse
 
christopheriolo profile image
Christophe Riolo

Thank you for sharing :)

I spotted a typo in exercise 16 : the function divide that you have given has labeled arguments (type dividend:int -> divisor:int -> int) but you use it without labels (int->int->int) :
let modulo ~dividend ~divisor = dividend - (divisor * divide dividend divisor)

You should instead write :
let modulo ~dividend ~divisor = dividend - (divisor * divide ~dividend ~divisor)

Collapse
 
swyx profile image
swyx

wew! guess so! thank you! will update. sorry about that!

Collapse
 
christopheriolo profile image
Christophe Riolo

I mean, it works just fine, but it is less ambiguous. For example:

divide divisor dividend

would give the wrong result, whereas:

divide ~divisor ~dividend

would give the correct one.

Collapse
 
christopheriolo profile image
Christophe Riolo

And here's an implementation of exercise 24 with refs:

let min_and_max lst =
    (* We get the first element as the base for min and max
       We are assured that the list is not empty, but else it would throw an exception *)
    let min = ref (List.hd_exn lst) in
    let max = ref (List.hd_exn lst) in
    List.iter ~f:(fun elem ->
        if elem < !min then min := elem;
        if elem > !max then max := elem
    ) lst;
    !min, !max
;;
Thread Thread
 
swyx profile image
swyx

nice, thank you very much!!