Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!apple!uokmax!d.cs.okstate.edu!norman From: norman@d.cs.okstate.edu (Norman Graham) Newsgroups: comp.lang.misc Subject: Re: On whether C has first-class composable functions Message-ID: <1991Jan6.171234.27734@d.cs.okstate.edu> Date: 6 Jan 91 17:12:34 GMT References: <1991Jan05.222639.6387@dirtydog.ima.isc.com> Organization: Oklahoma State University Lines: 44 From article <1991Jan05.222639.6387@dirtydog.ima.isc.com>, by karl@ima.isc.com (Karl Heuer): > In article <1991Jan5.182428.615@mathrt0.math.chalmers.se> augustss@cs.chalmers.se (Lennart Augustsson) writes: >>[In C extended with anonymous functions, we could write compose():] >> typedef int (*intintfun)(int); >> intintfun compose(intintfun f, intintfun g) { >> return (int ANONYMOUS(int x) { return f(g(x))) }; >> } >>...the point is that you cannot add unnamed functions in a natural way >>without getting the additional semantic power. > > I disagree. Since the return value of the function references objects that go > out of scope before the value would be used, I would expect this to analogous > to int *h(int i) { return &i; } , which is undefined. The example is correct. The construct int ANONYMOUS(int x) { return f(g(x)) } [1] creates a new function value that has the _values_ of f and g bound up in its definition. Since the _values_ of f and g are used, scope plays no part in the value created by [1]. Remember, names have scope-- values do not. Here is an example that might help: int foo (int a, int b) { return a + b; } Do you say 'The return value of foo() references objects that go out of scope before the value is used.'? Of course not. The expression a + b creates a new value; this new value has no reference to a or b. In the same way, the expression [1] creates a new value; this new value has no reference to f or g. (Of course, the _values_ of f and g are used to create the _new_ value.) This is really much easier to think about in a functional language-- especially one that allows curried functions and partial application. -- Norman Graham Standard Disclaimer Applies {cbosgd,rutgers}!okstate!norman