Created
October 19, 2011 12:02
-
-
Save benjamintanweihao/1298103 to your computer and use it in GitHub Desktop.
Awesome Continuation Passing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
p(n-1, k) + p(n-1,k-1) + p(n-1,k-2) | |
#include <stdio.h> | |
int pascal(int n, int k) { | |
int aux(int n, int k, int(*c)(int)) { | |
int c1(int ret1) { | |
int c2(int ret2) { | |
int c3(int ret3) { | |
return c(ret1+ret2+ret3) | |
} | |
// Third Recursive | |
return aux(n-1,k-2,c3) | |
} | |
// Second Recursive Call | |
return aux(n-1, k-1,c2); | |
} | |
// First Recursive Call | |
return aux(n-1,k,c2); | |
} | |
// Base Case | |
if ( k==0 || n ==k ) return c(1) ; | |
return aux(n-1,k) | |
int identity(int x) { return x; } | |
return aux(n,k,identify) ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment