F# tail call optimization issues on Mono

375 views Asked by At

I have an issues concerning the F# on mono. Im doing this course in functional programming at my university. In the course we are using F#, and I uses Xamarin as my editor.

The thing is that we had a lesson on tail recursion, as a tool for getting efficiency. But when you are not able to write your function tail recursive, we had to use continuous, such that we using the heap and not the stack.

This seems not to work on mono 3.10.0 with F# 3.1, I get an System.StackOverflowException. This should be impossible to get, due the continuous should use the heap.

let rec fibC n c = 
match n with 
|0 -> c 0 
|1 -> c 1 
|n -> fibC (n-1) (fun v1 -> fibC (n-2) (fun v2 -> c(v1+v2)))
1

There are 1 answers

0
Christian On BEST ANSWER

I tested a Fibonacci implementation passing an accumulator instead of a function (continuation) like this:

let fib n = 
   let rec _fib i (a,b) =
      match i with 
      | 0 -> a
      | _ -> _fib (i-1) (b, a+b)
   _fib n (0,1)

which worked fine on Mono, i.e. no stack overflow. So I guess it's only an issue with TCO when using continuations. There's a Xamarin ticket from June 2013 addressing this.