forked from geekwithguitars/lab4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacci.ml
39 lines (32 loc) · 845 Bytes
/
fibonacci.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(*
A collection of statements contained in a "Fibonacci" module.
*)
type length =
| Infinite
| Finite of int ;;
(* info -- A type containing some random information about the
Fibonacci sequence *)
type info = {
name : string; (* evocative name for this module *)
length : length; (* the number of years this idea will be important *)
inventor : string (* the inventor of the ideas in this module *)
} ;;
let name = "Fibonacci" ;;
let length = Infinite ;;
let inventor = "Leonardo of Pisa" ;;
let info = {
name;
length;
inventor;
} ;;
(* exists -- Predicate holds if n is an appropriate fibonacci index *)
let exists n =
n > 0 ;;
(* eval -- Return the n-th fibonacci number *)
let eval n =
let rec ev n =
if (n = 1 || n = 2)
then 1
else ev (n - 1) + ev (n - 2)
in
Some (ev n) ;;