25 lines
507 B
OCaml
25 lines
507 B
OCaml
|
let rec fact n =
|
||
|
if n < 1 then 1
|
||
|
else n * (fact (n-1))
|
||
|
|
||
|
let x = fact 5;;
|
||
|
(*
|
||
|
The double semicolon is needed above.
|
||
|
|
||
|
This may be related:
|
||
|
http://ocamlverse.net/content/faq_if_semicolon.html
|
||
|
*)
|
||
|
Printf.printf "%d" x
|
||
|
(* 120 *)
|
||
|
|
||
|
(*************************************************)
|
||
|
|
||
|
(* https://github.com/wiredsister/example-ocaml/blob/master/examples/factorial.ml *)
|
||
|
let rec fact2 = function
|
||
|
| 0 -> 1
|
||
|
| n when n>1 -> n * (fact2 (n-1))
|
||
|
| _ -> 0;;
|
||
|
|
||
|
Printf.printf "%d" (fact 6)
|
||
|
(* 720 *)
|