Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!elroy.jpl.nasa.gov!usc!apple!uokmax!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.misc Subject: Re: composition of functions Keywords: composition, function, first-class object Message-ID: <4801@goanna.cs.rmit.oz.au> Date: 20 Feb 91 07:04:05 GMT References: <1991Feb8.191014.6430@spool.cs.wisc.edu> <1991Feb15.230112.2902@csrd.uiuc.edu> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 27 In article <1991Feb15.230112.2902@csrd.uiuc.edu>, bliss@sp64.csrd.uiuc.edu (Brian Bliss) writes: > >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)). > you say > int (*f1)(), (*f2)(); > int retval (x) { (*f1)((*f2)(x));} > int (*compose (f, g))() > int (*f)(), (*g)(); > { f1 = f; f2 = g; return retval; } Spare me days! This completely (almost definitively) misses the point. If I do fg = compose(f, g); hk = compose(h, k); the later call to compose() should not invalidate the earlier result! This atrociously buggy pseudo-implementation *guarantees* that compose(compose(f,g),h)(x) will not work, which it would in a language with first-class functions. -- Professional programming is paranoid programming