Skip to main content
added 232 characters in body
Source Link
Chris
  • 5.5k
  • 1
  • 7
  • 39

Clearly as I'm sure you're aware, all of your non-tail-recursive solutions have pretty horrendous performance characteristics. But I want to look at one of your tail-recursive solutions.

(** Get the nth fibonacci number using lists and tail recursion. *) let fibo_list n = let rec loop n xs = if n < 3 then List.nth xs 1 else loop (n - 1) [List.nth xs 1; List.hd xs + List.nth xs 1] in loop n [1; 1] 

A list is a data structure that by its very nature has any number of elements. The lists you use always have two elements. This is a much better place to use a tuple.

let fibo_tuple n = let rec loop n (a, b) = if n < 3 then b else loop (n - 1) (b, a + b) in loop n (1, 1) 

If you're going to use a list, you can still clean it up with pattern-matching, rather than calling List.hd and List.nth.

let fibo_list n = let rec loop n [a; b] = if n < 3 then b else loop (n - 1) [b; a + b] in loop n [1; 1] 

You will be warned about non-exhaustive pattern-matching because again, lists are the wrong data structure to use here, but your code never creates a list with anything other than two elements.

Of course, if you know the warning can be safely ignored, you can suppress it.

let fibo_list n = let[@warning "-8"] rec loop n [a; b] = if n < 3 then b else loop (n - 1) [b; a + b] in loop n [1; 1] 

Clearly as I'm sure you're aware, all of your non-tail-recursive solutions have pretty horrendous performance characteristics. But I want to look at one of your tail-recursive solutions.

(** Get the nth fibonacci number using lists and tail recursion. *) let fibo_list n = let rec loop n xs = if n < 3 then List.nth xs 1 else loop (n - 1) [List.nth xs 1; List.hd xs + List.nth xs 1] in loop n [1; 1] 

A list is a data structure that by its very nature has any number of elements. The lists you use always have two elements. This is a much better place to use a tuple.

let fibo_tuple n = let rec loop n (a, b) = if n < 3 then b else loop (n - 1) (b, a + b) in loop n (1, 1) 

If you're going to use a list, you can still clean it up with pattern-matching, rather than calling List.hd and List.nth.

let fibo_list n = let rec loop n [a; b] = if n < 3 then b else loop (n - 1) [b; a + b] in loop n [1; 1] 

You will be warned about non-exhaustive pattern-matching because again, lists are the wrong data structure to use here, but your code never creates a list with anything other than two elements.

Clearly as I'm sure you're aware, all of your non-tail-recursive solutions have pretty horrendous performance characteristics. But I want to look at one of your tail-recursive solutions.

(** Get the nth fibonacci number using lists and tail recursion. *) let fibo_list n = let rec loop n xs = if n < 3 then List.nth xs 1 else loop (n - 1) [List.nth xs 1; List.hd xs + List.nth xs 1] in loop n [1; 1] 

A list is a data structure that by its very nature has any number of elements. The lists you use always have two elements. This is a much better place to use a tuple.

let fibo_tuple n = let rec loop n (a, b) = if n < 3 then b else loop (n - 1) (b, a + b) in loop n (1, 1) 

If you're going to use a list, you can still clean it up with pattern-matching, rather than calling List.hd and List.nth.

let fibo_list n = let rec loop n [a; b] = if n < 3 then b else loop (n - 1) [b; a + b] in loop n [1; 1] 

You will be warned about non-exhaustive pattern-matching because again, lists are the wrong data structure to use here, but your code never creates a list with anything other than two elements.

Of course, if you know the warning can be safely ignored, you can suppress it.

let fibo_list n = let[@warning "-8"] rec loop n [a; b] = if n < 3 then b else loop (n - 1) [b; a + b] in loop n [1; 1] 
Source Link
Chris
  • 5.5k
  • 1
  • 7
  • 39

Clearly as I'm sure you're aware, all of your non-tail-recursive solutions have pretty horrendous performance characteristics. But I want to look at one of your tail-recursive solutions.

(** Get the nth fibonacci number using lists and tail recursion. *) let fibo_list n = let rec loop n xs = if n < 3 then List.nth xs 1 else loop (n - 1) [List.nth xs 1; List.hd xs + List.nth xs 1] in loop n [1; 1] 

A list is a data structure that by its very nature has any number of elements. The lists you use always have two elements. This is a much better place to use a tuple.

let fibo_tuple n = let rec loop n (a, b) = if n < 3 then b else loop (n - 1) (b, a + b) in loop n (1, 1) 

If you're going to use a list, you can still clean it up with pattern-matching, rather than calling List.hd and List.nth.

let fibo_list n = let rec loop n [a; b] = if n < 3 then b else loop (n - 1) [b; a + b] in loop n [1; 1] 

You will be warned about non-exhaustive pattern-matching because again, lists are the wrong data structure to use here, but your code never creates a list with anything other than two elements.