Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if function is declared recursive

Tags:

f#

Is it possible to check if a function was declared as recursive, i.e. with let rec?

I have a memoize function, but it doesn't work with arbitrary recursive functions. I would like to give an error if the user calls it with such a function. (Feel free to tell me if this is a bad way to deal with errors in F#)

like image 352
MariusUt Avatar asked Mar 15 '23 22:03

MariusUt


1 Answers

F# code is compiled to .NET Common Intermediate Language. F# rec keyword is just a hint to F# compiler that makes identifier that is being defined available in the scope of the function. So I believe that there is no way to find out at runtime that the function is declared as recursive.

However you could use System.Diagnostic.StackTrace class (https://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx) to get and analyze stack frames at runtime. But accessing stack information have significant performance impact (I'm assuming that your memoization function is for speeding up program performance). Also stack information could be different in Debug and Release versions of your program as the compiler can make optimizations.

like image 79
Petr Avatar answered Mar 28 '23 12:03

Petr