Discussion:
Mutually recursive local functions.
Elifant
2007-07-03 07:45:43 UTC
Permalink
Hello all.

Is it possible to write mutually recursive local functions in Nemerle?

def a()
b()

def b()
a()

gives "unbound name 'b'"

Known workarounds:
1. make them class methods (need to explicitly type them)
2. pass one as parameter to other (ugly)

Are there other possibilities?
Sergei Tulentsev
2007-07-03 08:24:24 UTC
Permalink
def a(p)
{
when(p > 0)
b(p - 1);
}
and b(p)
{
WriteLine(p);
a(p);
}
Post by Elifant
Hello all.
Is it possible to write mutually recursive local functions in Nemerle?
def a()
b()
def b()
a()
gives "unbound name 'b'"
1. make them class methods (need to explicitly type them)
2. pass one as parameter to other (ugly)
Are there other possibilities?
_______________________________________________
https://nemerle.org/mailman/listinfo/devel-en
--
Best wishes,
Sergei Tulentsev
Senior software engineer.

SITRONICS Telecom Solutions, Moscow
http://www.sitels.ru
mob: + 7 916 507 7112
Elifant
2007-07-03 09:40:32 UTC
Permalink
Post by Sergei Tulentsev
def a(p)
{
when(p > 0)
b(p - 1);
}
and b(p)
{
WriteLine(p);
a(p);
}
Cool! This is definitely must be somewere on the Wiki. May be it is, but
I couldn't find.
Michal Moskal
2007-07-03 10:37:30 UTC
Permalink
Post by Elifant
Cool! This is definitely must be somewere on the Wiki. May be it is, but
I couldn't find.
Go ahead and add it then :)
--
Micha³
Elifant
2007-07-03 11:40:10 UTC
Permalink
Post by Michal Moskal
Post by Elifant
Cool! This is definitely must be somewere on the Wiki. May be it is, but
I couldn't find.
Go ahead and add it then :)
I've added it at
http://nemerle.org/Grok_Base_structure_of_programs#Local_functions,
but I can't think out simple and useful example of use. So I just wrote
dumb code.
Radu Grigore
2007-07-03 12:31:06 UTC
Permalink
Post by Elifant
I've added it at
http://nemerle.org/Grok_Base_structure_of_programs#Local_functions,
dumb code.
One (common?) use is to log the parameters and the return value of a
recursive function.

def fib(n)
if (n<2) n
else fib(n-1)+fib(n-2)

gets changed into

def fibNoLog(n)
if (n<2) n
else fib(n-1)+fib(n-2)
and fib(n)
def r = fibNoLog(n)
WriteLine($"fib($n)=$r")

The transformation is automatic and easy to do. Modifying a
complicated recursive function might not be as convenient especially
if it has multiple return points.
--
regards,
radu
http://rgrig.blogspot.com/
Loading...