Talk:Function composition

Is this task subject to the "limitation" of First-class functions, ruling out C (or Fortran, or...), or we can accomplish the task implementing just a function that returns f(g(x)), having as argument f, g and x? --ShinTakezou 15:17, 3 March 2009 (UTC)

Hi, the limit is you have to create a function of f and g that returns another function. It is that other function, when applied to x would be the same as doing f(g(x)). If you look at the Python example, function compose returns function sin_cos. it is then sin_cos(x) that is equivalent to sin(cos(x)). In short, you need to create function compose. Thanks. --Paddy3118 02:54, 4 March 2009 (UTC)
I.e. this is possible only for languages that have First-class functions (by the way, it seems like this task is already covered by showing that the language has first class functions in First-class functions task page) --ShinTakezou 11:27, 4 March 2009 (UTC)
Thats right, it is another aspect of first class functions but there is no need to show functions as members of other collection types. Some languages may be able to do this and not First Class Functions. --Paddy3118 15:48, 4 March 2009 (UTC)
Let me show another doubt of mine. What a function is for a language, is definible inside the same language; C can deal naturally with pointers, and we pass function to e.g. other functions by pointer (reference?); so foo(bar) call foo with argument bar (by the way, "calling a subroutine" for compiled languages means always to know its address, even at the end of the games a run-time, such the one of Objective-C when "finding" the code for a selector, compute the address of other compiled code...), and "foo" is the function (which C handles by pointer). So this task, if the right constraint of First-class functions is dropped (exactly the first: "New functions can be created from others at run time"), can be implemented in C. If it is not droppable, languages can accomplish this task iff they "have" first class functions. Am I reasonably right? --ShinTakezou 16:23, 4 March 2009 (UTC)


IF you can create a version of compose that works for function pointers when you have an arbitrary set of two functions to compose then why not put it down? I add the extra restriction because if you had n sets of f and g functions to compose, i.e.
 composeN = compose(fN, gN) for N in 0..N-1 
Then any of the composeN should be able to be used after all have been created. I can think of a naive implementation using function pointers where the last call to compose would reset the actions of all the composeN - this is not what is meant. My C is a little rusty but I guess if you could do something like:

<lang c> fg = compose(&f, &g)

 (*fg)(x) /* Where (*fg)(x) == f(g(x)) and another call to compose would leave this one alone */</lang>
--Paddy3118 19:27, 4 March 2009 (UTC)
Return to "Function composition" page.