diff --git a/OCaml/FibonacciSequence.ml b/OCaml/FibonacciSequence.ml new file mode 100644 index 0000000..50c63cf --- /dev/null +++ b/OCaml/FibonacciSequence.ml @@ -0,0 +1,36 @@ +(* Fibonacci Sequence *) +(* 2024.12.30 *) + +(* 1. Standard recursive method *) +let rec fib_recursive n = + if n <= 2 then 1 + else fib_recursive (n - 1) + fib_recursive (n - 2) + +(* 2. Tail recursive method *) +let fib_tail_recursive n = + let rec aux n a b = + match n with + | 1 -> a + | 2 -> b + | _ -> aux (n - 1) b (a + b) + in aux n 1 1 + +(* 3. Bottom-up method *) +let fib_bottom_up n = + if n <= 2 then 1 + else + let rec loop a b count = + if count = n then b + else loop b (a + b) (count + 1) + in loop 1 1 2 + +(* Function to print Fibonacci sequences *) +let print_fib_sequence name sequence = + Printf.printf "%s : %s\n" name (String.concat ", " (List.map string_of_int sequence)) + +(* Calculate and print the first 10 terms of the Fibonacci sequence using each method *) +let () = + let range = List.init 10 (fun x -> x + 1) in + print_fib_sequence "Standard Recursive Method" (List.map fib_recursive range); + print_fib_sequence "Tail Recursive Method " (List.map fib_tail_recursive range); + print_fib_sequence "Bottom-up Method " (List.map fib_bottom_up range) diff --git a/OCaml/README.md b/OCaml/README.md new file mode 100644 index 0000000..7fb3d41 --- /dev/null +++ b/OCaml/README.md @@ -0,0 +1,73 @@ +# [My OCaml Practice](../README.md#my-ocaml-practice) + + +### References + +- https://ocaml.org/ +- https://en.wikipedia.org/wiki/OCaml + + +### \ + +- Execute using *Version 5.0.0* in [JDoodle](https://www.jdoodle.com/compile-ocaml-online), unless otherwise specified + + +### \ + +- [Fibonacci Sequence (2024.12.30)](#fibonacci-sequence-20241230) + + +## [Fibonacci Sequence (2024.12.30)](#list) + +- Generate the Fibonacci sequence using regular recursive, tail-recursive and botthom-up methods +- Code and Output +
+ Code : FibonacciSequence.ml + + ```ocaml + (* 1. Standard recursive method *) + let rec fib_recursive n = + if n <= 2 then 1 + else fib_recursive (n - 1) + fib_recursive (n - 2) + + (* 2. Tail recursive method *) + let fib_tail_recursive n = + let rec aux n a b = + match n with + | 1 -> a + | 2 -> b + | _ -> aux (n - 1) b (a + b) + in aux n 1 1 + + (* 3. Bottom-up method *) + let fib_bottom_up n = + if n <= 2 then 1 + else + let rec loop a b count = + if count = n then b + else loop b (a + b) (count + 1) + in loop 1 1 2 + ``` + ```ocaml + (* Function to print Fibonacci sequences *) + let print_fib_sequence name sequence = + Printf.printf "%s : %s\n" name (String.concat ", " (List.map string_of_int sequence)) + ``` + ```ocaml + (* Calculate and print the first 10 terms of the Fibonacci sequence using each method *) + let () = + let range = List.init 10 (fun x -> x + 1) in + print_fib_sequence "Standard Recursive Method" (List.map fib_recursive range); + print_fib_sequence "Tail Recursive Method " (List.map fib_tail_recursive range); + print_fib_sequence "Bottom-up Method " (List.map fib_bottom_up range) + ``` +
+
+ Output + + ```ocaml + Standard Recursive Method : 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 + Tail Recursive Method : 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 + Bottom-up Method : 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 + ``` +
diff --git a/README.md b/README.md index 4515758..9894cdc 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ - [My **F#** Practice](#my-f-practice) - [My **Elixir** Practice](#my-elixir-practice) - [My **Haskell** Practice](#my-haskell-practice) +- [My **OCaml** Practice](#my-ocaml-practice) - [My **Racket** Practice](#my-racket-practice) - [My **Scala** Practice](#my-scala-practice) - [My **Scheme** Practice](#my-scheme-practice) @@ -41,6 +42,11 @@ - [Fibonacci Sequence (2024.04.01)](/Haskell/README.md#fibonacci-sequence-20240401) +## [My OCaml Practice](#list) + +- [Fibonacci Sequence (2024.12.30)](/OCaml/README.md#fibonacci-sequence-20241230) + + ## [My Racket Practice](#list) - [Fibonacci Sequence (2024.12.18)](/Racket/README.md#fibonacci-sequence-20241218)