Since OCaml is not a pure functional language, there are many ways to do this. This is how I write this code, just for a specific example.
let rec mylength list = (* DEBUG *) let () = Printf.printf "mylength here, null list: %b\n%!" (list = []) in (* DEBUG *) match list with | [] -> 0 | _ :: rest -> 1 + mylength rest
After that, you can delete the material inside the comments (* DEBUG *).
Pay attention to the use of%! to flush the buffer. If you are debugging a lot with printf (like me), it is very useful to know about this.
Jeffrey scofield
source share