Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!samsung!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: composition of functions Keywords: composition, function, first-class object Message-ID: <4771@goanna.cs.rmit.oz.au> Date: 15 Feb 91 03:22:59 GMT References: <1991Feb8.191014.6430@spool.cs.wisc.edu> <1991Feb13.212250.27437@csrd.uiuc.edu> Followup-To: comp.lang.misc Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 22 In article <1991Feb13.212250.27437@csrd.uiuc.edu>, bliss@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