Function composition

Erik Max Francis max at alcyone.com
Wed Aug 2 20:48:27 EDT 2000


Cees de Groot wrote:

> I was showing a Math PhD/Functional programming guy Python, and of
> course he
> was interested in map, filter, etcetera. He talked about function
> composition, ...
> ... but is there an easy way to extend this to any argument
> signature?

I think you have to start by specifying exactly what you want to have
happen in the case of multiple arguments.  Formally composition takes
two functions f: A -> B and g: B -> C and turns them into a new
function:

    h = f circle g* = g(f(a))

where a in A and h(a) in C.

To simplify things, have both of these functions have the domain and
range over the same set X (say, the real numbers, or the integers,
etc.).  So now f, g, f circle g: X -> X, and still f circle g = g(f(x)).

Now extend this simplified version to multiple arguments.  So instead of
f, g mapping X into itself, they map X cartesian X = X^2 into X.  But in
that case f circle g doesn't make any sense, because g expects a value
of an X^2 and the return value of f is an X.

One could come up with a fairly reasonable meaning to multivariate
function composition with f, g: X^2 -> X^2, but that probably wasn't
what you had in mind.  (Python has single return values only, so you'd
have to return 2-tuples, or something.)

It's not that it's impossible to come up with some general form of
function composition, it's just that there are different ways to do it,
and without any further clues one can't guess which one would be better.

. 

* Notations vary on this; sometimes the composition of f and g is
written f circle g, sometimes g circle f.  Either way it is easy to sort
out, because their domains and ranges are different sets, so their
composition must be g(f(x)), as f(g(x)) would be nonsensical.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Sometimes we're blinded by the light / If we'd only use our eyes
\__/ Oleta Adams
    Alcyone Systems / http://www.alcyone.com/
 Alcyone Systems, San Jose, California.



More information about the Python-list mailing list