Break a loop in OCaml

后端 未结 1 1083
臣服心动
臣服心动 2021-01-15 01:25

I often need to break a loop in OCaml, there are at least two ways:

(* by exception *)
try
  for i = 0 to 100 do
    ...
    if cond then raise BreakLoop
  d         


        
相关标签:
1条回答
  • 2021-01-15 02:29

    Compared to other languages, exceptions are very fast in ocaml (as long as you use the original compilers. Things are different for js_of_ocaml, ocaml-java, etc.)

    However, the solution with compilicated while-loops will still be a little bit faster. I wouldn't care about the mininmal speed differences, if the code is easier to read with exceptions - at least in most cases.

    0 讨论(0)
提交回复
热议问题