2009년 10월 15일 목요일

OCaml Lessons

http://www.abc.se/~m10217/ocaml_lessons.htm

여기에서 가져옴.
9년 동안 OCaml을 쓴 사람이 얻은 교훈이니 기억하자!
----

O'Caml Lessons

I have been programming O'Caml since May 2000. If you do as I say below, you will spend much less time debugging your O'Caml-programs than I do/did.

  • Read documentation carefully before sending bug report.
    There is a big difference between Str.regexp and Str.regexp_string.

  • Simple tail-recursive functions are the fastest.
    Don't spend time creating for-loops with exceptions or inner recursive functions. See the file other_compares.ml for concrete examples.

  • Try to make the return type explicit.
    By writing 'f a b : int', you will find missing argument errors much easier.
  • Don't forget the '- 1' in for-loops.
  • Add lots of assert.
  • Use O'Camldebug to understand why your asserts failed. You should use the cygwin-version on Windows during development.
  • Use records, do not use tuples.
    By having a lot of fst and snd in your code, you loose a lot of the type checking advantage.
  • O'Caml isn't a pure language! There are side-effects.
    When you change strings and arrays, if the original string is still needed, don't forget to copy it before destroying them.
  • Array.sort sorts in-place!
  • Always let Emacs indent!
    Otherwise code using if-then-else and match might not do want you think.
  • Don't write let _ = xxx if you know it should be let () = xxx
  • Add 'flush stdout' after debug outputs.
    If you get an assert the output buffer will not be emptied, so you might actually think the program fails earlier than it does.
  • Use a Makefiles, for example OcamlMakefile.

댓글 없음:

댓글 쓰기