composition of functions

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Fri Feb 15 14:22:59 AEST 1991


In article <1991Feb13.212250.27437 at csrd.uiuc.edu>, bliss at sp64.csrd.uiuc.edu (Brian Bliss) writes:
> 	(2) Composition is a difficult notion to express in C.
> 2)
>    say you have two functions,
>    int f (int x);    int g (int x);
>    what's so hard about writing:
>    int fg (int x) { return (f(g(x)); }

There's nothing hard about it, but it answers the wrong question.
The question was:  how do I write a function
	typedef int i2ifn(int);
	i2ifn compose(i2ifn f, i2ifn g) { /* fill this in */ }
so that compose(f,g)(x) == f(g(x)).  In Scheme, it's trivial:
	(define (compose f g) (lambda (x) (f (g x)) ))
The answer is that there isn't any way of doing it portably in C where
i2ifns are plain ordinary C functions of the indicated type.  There are
various kludges that let you work with things that are not *C* functions,
and there are some machine-specific hacks, but portable plain C, no.
(The best answer is probably to use Scheme->C.)

-- 
Professional programming is paranoid programming



More information about the Comp.lang.c mailing list