Skip to content

Instantly share code, notes, and snippets.

@WojciechKarpiel
Created January 9, 2021 11:08
Show Gist options
  • Select an option

  • Save WojciechKarpiel/a9e4a72557b189b16d9d4a117d3ff44c to your computer and use it in GitHub Desktop.

Select an option

Save WojciechKarpiel/a9e4a72557b189b16d9d4a117d3ff44c to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdint.h>
typedef uint64_t calkowita;
typedef calkowita (fn)(void*,calkowita);
calkowita razy_n(void* dane, calkowita c){
calkowita x = *((calkowita*) dane);
return x*c;
}
/*
Słkada 2 funkcje
w danych są po kolei wskaźniki na:
fn1, fn2, dane1, dane2
zwraca \c -> (fn1 dane1 (fn2 dane2 c))
*/
calkowita zloz(void *dane, calkowita c) {
void **xd;
xd = dane;
fn *fn1 = xd[0];
fn *fn2 = xd[1];
void *d1 = xd[2];
void *d2 = xd[3];
calkowita c2 = fn2(d2,c);
calkowita c1 = fn1(d1,c2);
return c1;
}
calkowita silnia_csp(calkowita (*k)(void*,calkowita),void *dane, calkowita n){
if (n<=1) return k(dane,1);
void *d[4];
d[0]=&razy_n;
d[1]=k;
d[2]=&n;
d[3]=dane;
return silnia_csp(&zloz,d,n-1);
}
calkowita id(void *dane, calkowita n){
(void) dane;
return n;
}
int main(){
calkowita n, w;
for(n=0; n < 16; n++){
w=silnia_csp(&id,0,n);
printf("%2lu! = %lu\n",n, w);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment