Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
K combinator - Lazy evaluation
#include <assert.h> #include <stdio.h> #define STACK_SIZE 50000 /* a boxed value is a function that can be executed to compute something. */ typedef void (*Boxed)(); /* a return continuation that receives a boxed value. */ typedef void (*BoxedContinuation)(Boxed); /* A return continuation that receives an int value. */ typedef void (*IntContinuation)(int); /* Custom stack allocated on the .data section*/ void *stack[STACK_SIZE]; /* Stack pointer */ int sp = 0; /* Push a continuation `cont` */ void pushContinuation(void *cont) { assert(sp >= 0); assert(sp < STACK_SIZE); stack[sp] = cont; sp++; } /* Pop a continuation frame. */ void *popContinuation() { assert(sp < STACK_SIZE); assert(sp >= 0); sp--; void *cont = stack[sp]; return cont; } /* one = 1# */ void one() { printf("in function: %s\n", __FUNCTION__); void *f = popContinuation(); (*(IntContinuation)(f))(1); } /* bottom = bottom */ void bottom() { printf("in function: %s\n", __FUNCTION__); bottom(); } /* K x y = x */ void K(Boxed x, Boxed y) { printf("in function: %s\n", __FUNCTION__); void *f = popContinuation(); (*(BoxedContinuation)(f))(x); } void XForceContinuation(int i) { printf("in function: %s\n", __FUNCTION__); printf("%d", i); } void KContinuation(Boxed x) { printf("in function: %s\n", __FUNCTION__); pushContinuation((void *)XForceContinuation); x(); } int main() { printf("in function: %s\n", __FUNCTION__); pushContinuation((void *)KContinuation); K(one, bottom); return 1; }
run
|
edit
|
history
|
help
0
move_string
Standard Template Library
CS1428 SI Tuesday
Bubble Sort Example
Non type template argument
khcmknhc
std::99 bottles of beer!
ECE2574_Function_Calling_Example
list iota cout
overloadresolution