Recursion Applied using Delegates & Lambda Expressions

Yesterday, I came across an excellent article on “Anonymous Recursion in C#” by Wesdyer, I thought I’ll give a try on applying Delegates and Lambda Expressions in solving a recursive problem.

Lambda Expressions are well suited in solving mathematical problems (apart from other domains). One of the basic problem we encountered in our early mathematics is a factorial. A factorial is represented as factoral(n) => n! = n(n-1)(n-2)! where factorial(0) = 1. If you look at it, it is recursive by nature. We usually solve it using a recursive method in C# like this.

Code Snippet
  1.     /// <summary>
  2.     /// Factorial is represented as n! = n(n-1)! or n! = n(n-1)(n-2)! so on...
  3.     /// and you can see, it is recursive by nature.
  4.     /// Also factorial of 0 is 1 [base case] i.e. Recursion limit/end condition.
  5.     /// </summary>
  6.     public static long Factorial(long n)
  7.     {
  8.         return n > 1 ? n * Factorial(n - 1) : 1;
  9.     }
  10. }

Here is my take on it using a generic and recursive delegate and a lambda expression. (For functional programming and lambda calculas read here [Wiki]).

Code Snippet
  1. delegate TResult RecursiveDelegate<TArg,TResult>( RecursiveDelegate<TArg,TResult> f, TArg arg );
  2. public static void FactorialTest()
  3. {
  4.     RecursiveDelegate<long, long> factorial = (f, n) => n > 1 ? n * f(f, n - 1) : 1;
  6.     // Look how I called the recursive delegate.
  7.     Console.WriteLine(factorial(factorial, 5)); // The answer is 120.
  8. }

I defined a generic recursive delegate namely RecursiveDelegate<TArg,TResult>, that takes two arguments, one is reference to itself and the other one is, argument of generic type. The Factorial method, defined earlier, is re-written in the form of Lambda Expression, see Line#4 above. Also notice, how the recursion is invoked, line#7. Isn't it beautiful, absolutely is I’ll come to the Lambda Expressions once again when I’ll discuss about LINQ. So stay tuned.

If you enjoyed reading this blog, leave your valuable feedback and consider subscribing to the RSS feed. You can also subscribe to it by email. Also, you can follow me on Twitter. Thank you!

Comments (4) -

payday loans online
12/7/2017 10:42:35 PM #

loans  payday advance online -  pay day loans  payday loans bad credit -

sexiga tjejer som hånglar youtube
12/8/2017 6:25:12 AM #

Gratis erotik Sexvideos, sexiga bilder & sexnoveller.

online loan
12/8/2017 7:48:09 AM #

cash loans no credit check  pay day loan -  cash loans no credit check  payday loans for bad credit -
12/8/2017 8:21:11 AM #

Simply wanna remark on few general things, The website pattern is perfect, the articles is rattling excellent Laughing.  Feel free to visit my webpage - bentley br1 ( - )

Hurrah! After all I got a website from where I be able to truly get useful facts regarding my study and knowledge.  Visit my website ::  Informacion Sobre Los Accidentes Automovilisticos -
12/8/2017 8:17:09 PM #

Possibly. However do you have a web method?

loans for bad credit
12/9/2017 1:37:18 AM #

no credit check loans  payday loans no credit check -  payday loans no credit check  payday loans no credit -  payday loans no credit check

Skin Daily
12/9/2017 7:56:12 AM #

I every time spent my half an hour to read this blog's content everyday along with a cup of coffee.  my blog post:  Skin Daily -

car market
12/9/2017 9:35:56 AM #

You could certainly see your expertise in the work you write. The sector hopes for more passionate writers like you who are not afraid to say how they believe. All the time go after your heart.  Feel free to surf to my website  car market -

steroids before and after
12/9/2017 12:29:39 PM #

When somebody abuses  steroids before and after - , gender mix-ups take place.

medieval wedding dress
12/9/2017 9:36:35 PM #

Very good article. I certainly appreciate this site. Continue the good work!  Stop by my web site:  medieval wedding dress -

predator hunting
12/10/2017 5:14:30 AM #

Some truly prime articles on this website, bookmarked.  Here is my homepage -  predator hunting -

Some times its a pain in the ass to read what blog owners wrote but this site is really user genial!  Also visit my site;  pure garcinia cambogia extract reviews -

free online casino games
12/10/2017 7:12:07 AM #

free casino games no download  free casino games no download -  free casino slot games  casino slots -

Hmm is anyone else having problems with the pictures on this blog loading? I'm trying to determine if its a problem on my end or if it's the blog. Any feedback would be greatly appreciated.  Feel free to surf to my web blog -  Que Son Los Accidentes Automovilisticos California -

his latest blog
12/10/2017 6:07:39 PM #

Very good write-up. I absolutely appreciate this site. Continue the good work!

website video hosting
12/10/2017 6:46:22 PM #

server dedicated jdbc oracle  cheap dedicated server india -  vps server ukraine  website hosting packages uk -  dota 2 dedicated server forum  usa windows vps hosting -  web hosting domains for sale  ssd vps europe -  web hosting with live video streaming  best offshore vps -  free vps server  vps control panel virtual private server management made easy -  buy cheap dedicated server hosting  cheap vps hosting in europe -  free hosting with cpanel no ads  hosting wordpress site -  how to manage vps unmanaged  radio streaming server hosting -  web hosting service providers in hyderabad  best dedicated server deals -  hosting dedicated server germany  web hosting virtual server -  dedicated servers australia cheap  website design and hosting costs -  dedicated server cheap price  ssd cached vps -  vps hosting europe windows  dedicated reseller hosting -  ubuntu vps hosting  vps servers australia -  best uk hosting for wordpress  vps linux hosting -  web hosting pay by the month  best european vps hosting -  thailand dedicated server hosting  difference between dedicated server and vps -  managed wordpress hosting services  web hosting deal -  windows vps hosting usa  free game server hosting samp -  web hosting support php  best dedicated server germany -   website video hosting -  hosting australia wordpress  windows vps discount -  free vps windows no credit card  web hosting services unlimited bandwidth -  free trial dedicated server  netherlands dedicated servers -  web hosting for churches  web hosting top 10 indonesia -

linux vps unlimited bandwidth
12/10/2017 7:49:59 PM #

free hosting site for wordpress  dedicated server windows usa -  website hosts in kenya  website hosting shared server -  web hosting houston tx  vps ubuntu server -  asp domain hosting  cheap vps reseller -  germany dedicated server cheap  web hosting problems -  windows 2008 vps  dedicated server vs vps -  shared hosting packages  web host vs domain name -  web domain and hosting in india  web hosting discount -  vps windows hosting  web hosting cost philippines -  cheap master reseller hosting in india  web server on mac pro -  web hosting billed monthly  web hosting java support -  dedicated hosting server price  web hosting panel open source -  cpanel vps hosting  dedicated server hosting gpu -  windows server vps cheap  vps hosting reseller program -  best semi dedicated hosting  vps hostings -  dedicated windows server hosting cheap  best us dedicated servers -  web hosting service uk  cheap ssd vps -  vps hosting reviews  dedicated server windows 10 -  website hosting with email  dedicated server windows usa -  cheap  linux vps unlimited bandwidth -  vps unmetered  best dedicated server hosting australia -  what is reseller hosting account  european windows vps -  best wordpress hosting canada  website hosting services india -  website host  web hosting boca raton -  dedicated hosting services dubai  web hosting host europe -  cpanel for vps price  canada dedicated hosting -

cheapest domain hosting sites
12/10/2017 8:05:34 PM #

web hosting banner design  web hosting california -  free vps with windows  web hosting news articles -  web hosting coupon  web hosting hyderabad -  dedicated server vs vps  vps xen hosting -  reseller hosting plans  best dedicated server hosting in usa -  managed wordpress hosting cheap  game server control panel php -  web hosting ssh  fully managed wordpress hosting -  storage dedicated server  website hosting servers -   cheapest domain hosting sites -  and hosting reseller  web hosting unlimited domains india -  web hosting control panel  web host australia small business -  web server on linux vs windows  affordable windows vps -  dedicated server cheap rent  windows hosting wordpress -  dedicated hosting server usa  linux shared hosting ng -  cheap windows vps unlimited bandwidth  web hosting fees definition -  web hosting us server  fully managed dedicated hosting -  dedicated ip hosting india  free wordpress hosting -  email dedicated server  top vps hosting europe -  dedicated server box  cheap unmetered vps hosting -  how to host multiple wordpress sites on one server  whm reseller hosting cheap -  mysql dedicated server hosting  website hosting services in nigeria -  magento 2 shared server  vps 30 day trial -  best dedicated hosting services  free vps server indonesia -  uk vps server  web server joomla -  top 10 reseller hosting india  web hosting uk top 10 -  unmetered dedicated server australia  cheap vps us -

cheap dedicated server cpanel
12/10/2017 8:08:46 PM #

dedicated server unlimited bandwidth  web hosting bandwidth speed -  vps hosting unlimited bandwidth  free ftp server hosting -  dedicated server indonesia windows  reseller hosting program -   cheap dedicated server cpanel -  linux vps review  cpanel vps hosting uk -  wordpress hosting options  web hosting with email accounts -  fastest wordpress host  ssd vps hosting india -  shared hosting for laravel  web email hosting -  reseller hosting unlimited indonesia  offshore vps hosting cheap -  web hosting cpanel  web hosting provider uk -  master reseller hosting us  web hosting solutions uk -  best and cheap vps hosting  web hosting linux distro -  what is linux shared hosting  web hosting services greece -  dedicated media server  best domain and hosting reseller program -  low cost dedicated windows server  virtual server hosting windows sql -  russian offshore vps hosting  web hosting bandwidth test -  reseller host  web designing and hosting services -  free reseller host  dedicated servers cheap uk -  web host cheap uk  best dedicated servers in india -  best offshore dedicated server  website host definition -  cheap domain hosting australia  how to move a wp site to a new host -  european windows vps  best alpha reseller hosting -  web hosting process step by step  fastest wordpress hosting -  dedicated hosting server price  dedicated hosting ratings -  web hosting with iis  website hosting best -  what is self hosted wordpress  hosting wordpress blog -

top photo hosting sites
12/10/2017 8:33:25 PM #

windows vps ssd  cheap vps hosting switzerland -  what does dedicated server means  benefits of virtual private server hosting -  web hosting reseller uk  vps linux server uk -  web design hosting business model  cheap dedicated servers europe -  best wordpress hosting sites  low cost windows vps -  vps dedicated server  what's a non dedicated server -  cheap australian vps  best shared hosting with cpanel -  cheap uk cpanel vps  whm vps file manager -  australian windows vps  affordable vps hosting cpanel -  how to host html site in iis  web hosting uk reviews -  windows based vps hosting  dedicated server ip kvm -  free wordpress hosting providers  web host email limit -  best cheap dedicated hosting  dedicated managed hosting -  vps server uk cheap  white label email hosting reseller -  how to move your wordpress blog to another host  low cost dedicated server india -  best kvm vps  master reseller hosting unlimited indonesia -  dubai dedicated server hosting  best canadian dedicated servers -  good cheap reseller hosting  wordpress hosts -  vps cpanel hosting  hosting reseller australia -  web hosting service navi mumbai  web hosting venezuela -  dedicated server multiple ip addresses  dedicated server offer -  web hosting joomla indonesia  web hosting through iis -  free blog hosting sites  web hosting prices in nigeria -  web radio  top photo hosting sites -  greece  cheap hosting with dedicated ip -  pure ssd reseller hosting  how to move your wordpress site to another host -

online casino no deposit bonus
12/11/2017 1:22:56 AM #

online casino games  free casino slot games -  casino slot games  casino slots -

12/11/2017 2:41:51 AM #

I am no longer positive where you are getting your info, but great topic. I needs to spend a while studying more or understanding more. Thank you for wonderful information I was looking for this information for my mission.

12/11/2017 9:13:44 AM #

In de platte seks op televisie gaan mensen geen relaties aan, ze gebruiken elkaar.

web hosting services reviews uk
12/11/2017 10:54:02 AM #

windows server 2008 r2 vps  web hosting options -  fastest shared hosting wordpress  web hosting cost philippines -  dedicated server hosting deals  fully managed dedicated servers -  dedicated servers defined  dedicated server hosting forum -  cheap dedicated hosting server  windows xp vps -  dedicated server hosting best price  what does non dedicated server mean -  dedicated server ssd germany  website hosting with coldfusion -  freebsd dedicated servers  best white label hosting reseller -  dedicated server advantages  free hosting for html -  xen vps servers  best ssd shared hosting -  best ubuntu vps hosting  wordpress blog hosting india -  singapore windows vps server  web hosting in delhi -  web hosting india best  web hosting scotland -  websites hosted on ip  virtual vs dedicated hosting -  reseller hosting server indonesia  semi dedicated hosting vs vps -  dedicated email server  web hosting with wordpress support -  dedicated server hosting europe  wordpress linux hosting -  web hosting coupon codes  dedicated servers cheapest -  web hosting usa best  anti ddos dedicated servers europe -  web hosting appliance  free domain email hosting uk -  cheap windows vps servers  free unlimited vps hosting -  wordpress host files for download  web hosting linux server -  shoutcast reseller hosting  vps managed indonesia -  website hosting package  website domain and hosting reviews -   web hosting services reviews uk -  service hosting  cheap australian dedicated server hosting -
12/11/2017 12:06:27 PM #

E) préstamo de la construcción. Verónica Anzures / Del.

dedicated servers managed  dedicated or non dedicated server -  best hosting for multiple wordpress sites  what do you mean by dedicated server -  cheapest windows vps  free vps servers -  windows rdp vps hosting  cheapest domain hosting canada -  offshore windows vps  free wordpress hosting australia -  best hosts for wordpress  web hosting windows or linux -  website development hosting agreement  dedicated hosting reviews -  virtual server vs dedicated server  best windows vps service -  web hosting why choose us  dedicated streaming servers -  fastest hosting for wordpress sites  australian reseller hosting plans -  web hosting malaysia wordpress  vps ubuntu server 12.04 -  Web server joomla  web hosting new zealand -   cheap dedicated server hosting in india -  domain hosting sites  linux vps with gui -  cheap windows vps hosting plans  netherlands vps hosting server -  uk vps server hosting  web hosting online -  web hosting linux windows difference  dedicated hosting with cpanel -  cheap dedicated server malaysia  best dedicated servers uk -  web hosting sales  best reseller hosting australia -  alpha master reseller hosting  vps vds dedicated server -  best hosting wordpress europe  best vps services -  ddos protection dedicated server  hosting a wordpress blog -  web hosting for contractors  web hosting mailman -  web hosting top 100  windows vps server canada -  dreamweaver domain hosting  website hosting price per year -  best canadian reseller hosting  website hosting reseller accounts -

dedicated servers
12/11/2017 8:30:10 PM #

top shared hosting providers  dedicated gaming servers -  web hosting email storage  reseller wordpress hosting -  web application development and hosting  web designing and hosting in pakistan -  web hosting platform linux or windows  free server domain hosting -  vps management service  web hosting on raspberry pi 3 -  cheap vps server india  best deal vps hosting -  best fast wordpress hosting  web hosts that support wordpress -  virtual private server hosting singapore  windows vps promo code -  web host definition  web hosting thailand review -  us based vps hosting  cheap vps cpanel licence -  free vps hosting india  windows vps trial 30 days -  web hosting dallas  free hosting with ftp -  cheap dedicated server canada  cheap xeon dedicated server -  web hosting images  website hosting uk cost -  low cost  dedicated servers -  hosting  vps server demo -  website hosting services reviews  dedicated mysql server machine -  best cpanel reseller hosting  virtual server vs vps -  best dedicated hosting reviews  web hosting home page -  free blog hosting sites reviews  reseller hosting australian servers -  web host providers uk  vps ssd germany -  best price vps hosting  web hosting price philippines -  windows vps hosting japan  website hosting and design australia -  the best dedicated server  website hosting and design australia -  windows dedicated server cheap  best reseller hosting provider -  web hosting macedonia  cheap vps cpanel licence -

no credit check loans
12/12/2017 2:35:00 AM #

payday loans no credit  cash loans no credit check -  cash loans no credit check  no credit check loans -  no credit check loans

ford fairlane accessories
12/12/2017 4:56:23 AM #

I have been surfing online more than 2 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my view, if all web owners and bloggers made good content as you did, the web will be a lot more useful than ever before.  Here is my blog ...  ford fairlane accessories -

Very good written article. It will be helpful to everyone who usess it, including yours truly Smile. Keep doing what you are doing - for sure i will check out more posts.  Have a look at my web blog ... shopping store ( - )

cho thue ban ghe sofa
12/13/2017 8:54:31 PM #

Nice post. I learn something totally new and challenging on sites I stumbleupon everyday. It will always be exciting to read articles from other authors and use something from other web sites.

12/14/2017 11:20:04 AM #

I'm not sure why but this blog is loading very slow for me. Is anyone else having this issue or is it a problem on my end? I'll check back later on and see if the problem still exists.  Feel free to visit my webpage ::  Christopher -
12/14/2017 11:46:41 PM #

My partner and I stumbled over here  different web page and thought I might check things out. I like what I see so now i am following you. Look forward to exploring your web page yet again.

I have read so many articles regarding the blogger lovers except this article is really a fastidious paragraph, keep it up.

Does Extenze Work On The First Day
12/15/2017 1:25:29 AM #

I know this site gives quality based posts and additional material, is there any other site which presents these things in quality?

online casino usa
12/15/2017 7:22:54 PM #

slot machines  casino slot games -  free casino slots  casino free games -  online casinos

Lebron James Shoes
12/17/2017 8:22:46 AM #

It appears many types come and go, but some could stay like Nike shoes, hoop earrings, boat sneakers, and others.

Add comment