Skip to content

Instantly share code, notes, and snippets.

@Xophmeister
Last active August 29, 2015 14:01
Show Gist options
  • Save Xophmeister/e745014ded101858d6c7 to your computer and use it in GitHub Desktop.
Save Xophmeister/e745014ded101858d6c7 to your computer and use it in GitHub Desktop.
Functional(ish) Fibonacci in C :)
#include <stdio.h>
int (*fn[2])(int);
int fib(int n) { return (*fn[n < 2])(n); }
int fibSeed(int n) { return 1; }
int fibRecur(int n) { return fib(n - 1) + fib(n - 2); }
void fibInit(void) {
fn[0] = fibRecur;
fn[1] = fibSeed;
}
int main() {
int i;
fibInit();
for (i = 0; i <= 25; ++i) {
printf("%2d %6d\n", i, fib(i));
};
return 0;
}
@Xophmeister
Copy link
Author

Hooray for function pointers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment