function composition in C

R. Kym Horsell kym at bingvaxu.cc.binghamton.edu
Thu Feb 28 18:27:57 AEST 1991


In article <ACHA.91Feb28002548 at DRAVIDO.CS.CMU.EDU> acha at CS.CMU.EDU (Anurag Acharya) writes:
>In article <6873 at munnari.oz.au> aet at 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.
>Nope. it is possible.
>int compose (foo,bar,baz)
>int (*foo) ();
>int (*bar) ();
>int baz;
>{
>   return ((*foo) ((*bar)(baz)));
> }

I don't think anyone was saying your method was not valid, but functional
composition returns a _function_ given two other functions.  This enables you 
to, e.g., assign the said composition to another function variable.

Can you write something like:

	int (*proc1)(int);
	int (*proc2)(int);
	int (*my_composition)();

	my_composition = compose(proc1,proc2);

so that later you can say:

	printf("%d\n",(*my_composition)(1));

There will be a problem (for a start) with the type for `compose()'.
If you can handle that one there are a few more examples I have in
mind (where the composed functions include local variables).

-kym



More information about the Comp.lang.c mailing list