-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathy_gcc.c
70 lines (55 loc) · 1.82 KB
/
y_gcc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <inttypes.h> /* uintmax_t */
#include <stdio.h> /* printf */
#include <stdlib.h> /* EXIT_SUCCESS */
typedef uintmax_t (*func)( uintmax_t ) ;
/* wrapping the function pointer type in a struct
is necessary because C does not support a
function pointer type that has its own type
as an argument */
typedef struct wrap {
void (*_)( struct wrap , void (*)( func ) ) ;
} wrap;
typedef void (*callback)( func ) ;
static void y( wrap gen , callback cb ) {
gen._( gen
, ({
void _( func f ) {
cb( f ) ;
};
_;
})
);
}
static void factorial_improver( wrap partial , callback cb ) {
cb( ({
uintmax_t _( uintmax_t n ) {
return n == 0 ? 1
: ({
uintmax_t rv = 1;
partial._(
partial
, ({
void _( func f ) {
rv = n * f( n - 1 );
};
_;
})
);
rv;
}) ;
};
_;
}) );
}
static void show( uintmax_t value ) {
printf( "%ju" "\n" , value ) ;
}
static void fifty( func factorial ) {
show( factorial( 50 ) );
}
int main( int argc , char *argv[] ) {
y( (wrap) { ._ = factorial_improver }
, fifty
);
return EXIT_SUCCESS;
}