Write a recursive function/program, to calculate either the Factorial or the Fibonacci number of a given non-negative integer, without explicitly implementing the base case(s).
Your program/function must return/output the correct value in a reasonable time.
ifstatement anywhere in your program/function, nor
switchnor short-if-syntax (
?:). Not even loops. Basically all control-flow statements are forbidden.
try, catchor exceptions of any kind.
Terminate the program (anything like
Use external sources (file, web, etc.)
Avoid recursion and return the proper value otherwise. (Your function must be recursive).
Use any of the standard loopholes.
- Use polymorphism, create threads, wrap the Integer type with your own class, or use any other mechanism your language or runtime has. (I can think of a solution that uses the lazy-evaluation, for instance). The function/program can be written as more than one function, using Mutual Recursion, if it helps you.
Please provide a short explanation of what you did.
Be creative! The winning answer is the one with the most upvotes by 2014-06-23 (a week from today).
Using Pattern Matching in Functional Programming Languages is OK since it's considered as Polymorphism. The solution should be trivial though, so I guess it won't have the chance to win, since it is a popularity-contest. It is still a legitimate answer.
Although some may say it's flow control in disguise, using lazy evaluation, (or short circuit evaluation) is allowed as well.