Path: utzoo!utgpu!cunews!cognos!alanm From: alanm@cognos.UUCP (Alan Myrvold) Newsgroups: comp.lang.misc Subject: Re: On whether C has first-class composable functions Message-ID: <9321@cognos.UUCP> Date: 12 Feb 91 19:32:27 GMT References: <7027:Feb906:42:0991@kramden.acf.nyu.edu> Reply-To: alanm@cognos.UUCP (Alan Myrvold) Organization: Cognos Inc., Ottawa, Canada Lines: 61 In article <7027:Feb906:42:0991@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes: > All seven working implementations I've seen here allow nested composition. Instead of all of the flamage, how about someone posting these 7 implementations for comment? ------------------------ int main() { if (main == &main) puts("C does not have first class functions"); } ------------------------ Obligatory compose implementation : ------------------------ #include typedef struct { int (*a)(),(*b)(),(*c)(); } c_f; c_f compose(a,b) int (*a)(),(*b)(); { c_f cf; cf.a = NULL; cf.b = a; cf.c = b; return cf; } int apply(cf,x) c_f *cf; int x; { if (!cf->a) { return apply(cf->b,apply(cf->c,x)); } else { return ((int (*)()) cf)(x); } } int square(x) int x; { return x*x; } int main() { c_f sqsq,sqsqsq; sqsq = compose(square,square); /* put square o square in sqsq */ sqsqsq = compose(square,&sqsq); /* put square o sqsq in sqsqsq */ printf("%d\n",apply(&sqsqsq,2)); /* apply sqsqsq to 2 and print it */ return 0; } ------------------------ Alan Myrvold 3755 Riverside Dr. uunet!mitel!cunews!cognos!alanm Cognos Incorporated P.O. Box 9707 alanm%cognos.uucp@ccs.carleton.ca (613) 738-1440 x5530 Ottawa, Ontario CANADA K1G 3Z4