Skip to content

OCaml : Fibonacci Sequence #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions OCaml/FibonacciSequence.ml
Original file line number Diff line number Diff line change
@@ -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)
73 changes: 73 additions & 0 deletions OCaml/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# [My OCaml Practice](../README.md#my-ocaml-practice)


### References

- https://ocaml.org/
- https://en.wikipedia.org/wiki/OCaml


### \<Note>

- Execute using *Version 5.0.0* in [JDoodle](https://www.jdoodle.com/compile-ocaml-online), unless otherwise specified


### \<List>

- [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
<details>
<summary>Code : FibonacciSequence.ml</summary>

```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)
```
</details>
<details open="">
<summary>Output</summary>

```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
```
</details>
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down