Xref: utzoo comp.lang.c:36518 comp.lang.functional:653 comp.theory:1581 Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!spool.mu.edu!sdd.hp.com!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!uwm.edu!csd4.csd.uwm.edu!markh From: markh@csd4.csd.uwm.edu (Mark William Hopkins) Newsgroups: comp.lang.c,comp.lang.functional,comp.theory Subject: Re: function composition in C Message-ID: <9754@uwm.edu> Date: 26 Feb 91 02:22:02 GMT References: <6873@munnari.oz.au> Sender: news@uwm.edu Followup-To: comp.lang.c Organization: University of Wisconsin - Milwaukee Lines: 53 In article <6873@munnari.oz.au> aet@felix.ee.mu.OZ.AU (bert) writes: > > Does anyone know how to write a compose function in C, >without writing a Scheme interpreter to do it in. > >A call should look something like this: > (compose(sqr,cube)) (2) >which would evaluate to 64. ... You could do it compile-time like this example for composing x^2 and 2*x: ------------------------------------------------------------ #include int Compose(F, G, X) int (*F)(), (*G)(); int X; { return (*F)((*G)(X)); } static int Square(X) int X; { return X*X; } static int Double(X) int X; { return 2*X; } static int Square_Double(X) int X; { return Compose(Square, Double, X); } static int Double_Square(X) int X; { return Compose(Double, Square, X); } main() { printf("%d, %d", Square_Double(5), Double_Square(5)); } ------------------------------------------------------------ The declarations above for Square_Double, and Double_Square would be compile-time lambda abstractions (with X being the variable abstracted out), but C does not support run-time lambda abstraction. You'd have to be able to write something like: int (*Compose(int (*F)(), int (*G)()))() { int (*H)(int X) { return (*F)((*G)(X)); } return H; } with embedded run-time function declarations... Brought to you by Super Global Mega Corp .com