Saturday, February 09, 2013

Useful Things to Know, #429: Recursive Anonymous Functions

If you’ve ever tried to code an anonymous function that calls itself, you may have concluded that it can’t be done.  After all, trying the following …

 Func<int, int> collatz = (n) =>
{
if (n == 1)
return 0;
if (n % 2 == 0)
return collatz(n/2) + 1;
else
return collatz(3 * n + 1) + 1;
};

… leads to the message:  “Error:  Use of unassigned local variable ‘collatz’”.

And this makes sense:  the delegate “collatz” doesn’t have a value until after the execution of the assignment statement in which you’re referencing it!

But the error message contains the key to making it work.  The message says that “collatz” is unassigned.  So in order to use it in an expression, assign it an initial value before assigning it your recursive anonymous function.

Func<int, int> collatz = null;
collatz = (n) =>
{
if (n == 1)
return 0;
if (n % 2 == 0)
return collatz(n/2) + 1;
else
return collatz(3 * n + 1) + 1;
};

(If you’re wondering what problem that function could possibly solve, check here.)

Labels:

12 Comments:

Blogger vijay antony said...

I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.


oracle training in bangalore

3/24/2018 4:38 AM  
Blogger rajatwebtrackker said...


Best Php Training Institute in Delhi
Best Java Training Institute in delhi
linux Training center in delhi
Web Designing Training center in delhi
Oracle Training Institute in delhi
blue prism Training Institute in delhi
Automation Anywhere Training center In delhi
rpa Training Institute in delhi
hadoop Training center in delhi

guys if you make your carrier and do what you want to do in your life so webtrackker is the best option to take your carrier make large

5/18/2018 7:52 AM  
Blogger brindhaajay said...

Good blog for oracle technology. Nice information given for the people.
Great job. keep it up.
oracle training in chennai
oracle training institute in chennai
oracle training in Coimbatore
best oracle training institute in Coimbatore
oracle training in bangalore
oracle training institutes in bangalore

12/21/2018 2:22 AM  
Blogger Unknown said...

Play online casinos with fun and money fun slots Fun money, take while there.

1/11/2019 6:00 AM  
Blogger Unknown said...

It's Really A Great Post. Looking For Some More Stuff.


shriramproperties-brake free|Pre Launch Electronic City Bangalore

2/03/2019 1:55 AM  
Blogger Unknown said...

It's Really A Great Post. Looking For Some More Stuff.


shriramproperties-brake free|Pre Launch Electronic City Bangalore

2/03/2019 1:56 AM  
Blogger Belago said...

Я могу расскзать где купить светодиодную ленту в Украине конечно же в ekodio отличный выбор тут

2/21/2019 3:32 AM  
Blogger Sadhana Rathore said...

Excellent post, it will be definitely helpful for many people. Keep posting more like this.
Automation Anywhere Training in Chennai
Automation courses in Chennai
Automation Training in Chennai
Blue Prism Training in Chennai
Blue Prism Training in Anna Nagar
RPA course in Chennai
RPA Training Institute in Chennai
RPA Training in Chennai

3/30/2019 5:00 AM  
Blogger prestige elysian said...

Information provided by you is very helpful and informative. Keep On updating such information.

prestige elysian


4/27/2019 2:55 AM  
Blogger Thoran said...

It is a very interesting and helpful informative blog, keep sharing.

Best Paper Products Company in Hyderabad | Thoran Paper Products

5/09/2019 5:01 AM  
Blogger Deva Dev said...

Informative and useful post thanks for sharing...
RPA training in bellandur
RPA training near bellandur
RPA courses in bellandur
RPA courses near bellandur
RPA classes in bellandur
RPA classes near bellandur
AWS training in bellandur
AWS training near bellandur
AWS courses in bellandur
AWS courses near bellandur
AWS classes in bellandur
AWS classes near bellandur

6/11/2019 8:01 AM  
Blogger ajish said...

Nice Post! Thank you for sharing very good post, it was so Nice to read and useful to improve my knowledge as updated one, keep blogging.
ASW training in Electronic City

6/17/2019 12:53 AM  

Post a Comment

<< Home