C# · Functional Programming

Functional Fibonnaci series in C#

C# code for functional fibonnaci numbers

Func<int, int> fib = null;
fib = n => n > 1 ? fib(n – 1) + fib(n – 2) : n;

Okay so you guys would think why would i declare the delegate in one line and instantiate in the next. The reason is that if you do everything in same line complier wont recognize the recursive call using the delegate. Try this 

Func<int, int> fib = n => n > 1 ? fib(n – 1) + fib(n – 2) : n;

Above code wont compile 

Haskell code for same 
fib n
| n == 0 = 0
| n == 1 = 1
| n > 1 = fib (n-1) + fib (n-2)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s