K
Khookie
Woah... is it just me or do C programmers don't bother talking about
how cool C can be (compared to Lisp, Haskell, etc.) - functionally
speaking?
// Lexical scoping - via nested functions
#include <stdio.h>
int main() {
int x = 10;
// lexical scoping
static void test() {
x = 123;
}
test();
printf("%d", x);
return 0;
}
// Closures
#include <stdio.h>
int triple(int num) {
return num * 3;
}
int square(int num) {
return num * num;
}
int main() {
int x = 10;
int (*func)(int);
func = square;
printf("%d\n", func(5));
func = triple;
printf("%d\n", triple(5));
return 0;
}
Chris
how cool C can be (compared to Lisp, Haskell, etc.) - functionally
speaking?
// Lexical scoping - via nested functions
#include <stdio.h>
int main() {
int x = 10;
// lexical scoping
static void test() {
x = 123;
}
test();
printf("%d", x);
return 0;
}
// Closures
#include <stdio.h>
int triple(int num) {
return num * 3;
}
int square(int num) {
return num * num;
}
int main() {
int x = 10;
int (*func)(int);
func = square;
printf("%d\n", func(5));
func = triple;
printf("%d\n", triple(5));
return 0;
}
Chris