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
Unlike C (even C99/C11), C++ allows initializers in if-conditions, so this compiles.
Recursive Call Example Sum
wellformed number
List comprehension in C++ using functional patterns
Fun with Pointers #2
Deleted special operations are propagated to derived class
the usual name hiding rules do apply with using directives
hello,world !ssn2019
std::set custom ordering with composition
STL Stack, C ++