Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!maverick.ksu.ksu.edu!ux1.cso.uiuc.edu!csrd.uiuc.edu!sp64.csrd.uiuc.edu!bliss From: bliss@sp64.csrd.uiuc.edu (Brian Bliss) Newsgroups: comp.lang.misc Subject: Re: composition of functions Keywords: composition, function, first-class object Message-ID: <1991Feb15.230112.2902@csrd.uiuc.edu> Date: 15 Feb 91 23:01:12 GMT References: <1991Feb8.191014.6430@spool.cs.wisc.edu> <1991Feb13.212250.27437@csrd.uiuc.edu> <4771@goanna.cs.rmit.oz.au> Sender: news@csrd.uiuc.edu (news) Reply-To: bliss@sp64.csrd.uiuc.edu (Brian Bliss) Organization: Center for Supercomputing Research and Development Lines: 37 >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); } if you don't like using global variables, then you have to say struct CLOSURE { int (*func)(); struct LOCALS *locals; } with the understanding that applying a closure is to say (*func)(locals, args...), and that any (would-be)local variables that the called function uses are found in the struct LOCALS. this is plain, portable C. bb