0

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)))
Andreas
  • 3
  • 2
  • There is some indication that Mono doesn't support tail call elimination, see [here](http://flyingfrogblog.blogspot.se/2009/01/mono-does-not-support-tail-calls.html) and [here](http://stackoverflow.com/questions/9595585/what-is-the-current-state-of-tail-call-optimization-for-f-on-mono-2-11) – Christian Nov 10 '14 at 20:24
  • I think you mean "continuation issues" on mono (in the title) but I cannot tell for sure so I won't change the title. – Onorio Catenacci Nov 10 '14 at 20:37
  • @OnorioCatenacci I guess it should be title something like "F# tail call optimization issues on Mono". The code above is as I understand it written to use a continuation as a means to get the recursive call in tail position and thus utilize TCO. – Christian Nov 10 '14 at 21:10
  • Your right about the title, I edited it. The code is an example of solution to without tail recursion with accumulating parameter. So this piece of code will solve fibonacci 40 on a windows .net machine without a problem. – Andreas Nov 10 '14 at 23:06

1 Answers1

0

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.

Christian
  • 7,433
  • 4
  • 36
  • 61