[ prog / sol / mona ]

prog


Monads,Async/Await : Algebraic Effects in C99

45 2021-01-15 07:27

//array version
#include "Util/void.h"
#define rolldarg(arr) ({double r=rdouble();double res=0;\
for(size_t i=0; i<sizeof(arr)/sizeof(arr[0]);i++){r-=arr[i]*0.01;\
if(r<0.0){res=arr[i];break;}};res;})

int main(int argc,char**argv){
double chances[]={50.1,20.0,29.9};
printf("%.2f%% chance",rolldarg(chances) );
// ./roll 50.1 20.0 29.9
// 50.10% chance
;}

69


VIP:

do not edit these