feat: add sample gcd

This commit is contained in:
2020-11-08 10:10:23 +08:00
parent 03aee66775
commit c36b20c671
3 changed files with 29 additions and 0 deletions

3
.gitignore vendored
View File

@@ -4,6 +4,9 @@
.AppleDouble
.LSOverride
*.cmi
*.cmo
# Icon must end with two \r
Icon

View File

@@ -4,6 +4,14 @@ OCaml Language tests
https://ocaml.org/
<br>
Compile single file:
```shell
$ ocamlc -o gcd gcd.ml
```
<br>
Install on macOS
```shell

18
gcd.ml Normal file
View File

@@ -0,0 +1,18 @@
(* File gcd.ml *)
let rec gcd a b =
if b = 0 then a
else gcd b (a mod b);;
let main () =
let len = Array.length Sys.argv in
Printf.printf "array length %d\n" len;
if len < 3 then (
Printf.printf "must have 2 args";
exit 0;
);
let a = int_of_string Sys.argv.(1) in
let b = int_of_string Sys.argv.(2) in
Printf.printf "gcd of %d and %d is %d\n" a b (gcd a b);
exit 0;;
main ();;