Variables

Donn Cave donn at drizzle.com
Sun Apr 24 13:32:21 EDT 2005


Quoth Mike Meyer <mwm at mired.org>:
...
| He's right - programming is an offshoot of mathematics. It adds
| *dynamics* to the structures of mathematics. In mathematics, a
| construct (graph, function, mapping, set, whatever) is immutable. You
| can talk about things that change with time, but you do so with a
| function f(t) that describes the changes to the thing over time - and
| *that* function is immutable. You can say that it isn't time that's
| changing, but frobnitz, with the same function describing the change -
| and you get the same structures that you got with time.
|
| This change causes a fundamental change in the way practitioners
| *look* at objects. Which is visible as a change in the
| vocabulary. Yes, you can talk about programming with the vocabulary of
| mathematics. But that's like dancing about architecture (*).

There's a school of thought that holds this to be kind of an offshoot
of programming, too, albeit a dominant one at present.  In a "pure"
functional programming language like Haskell for example, the "="
operator is equational in about the same way as it is in mathematics.

Even Haskell's equivalent of "foo = raw_input()", the notation that
appears to create a variable is strictly bound to an underlying system
of functions and equations that preserves this equational property,
and maybe for the sake of harmony between mathematicians this system
could be applied to Python's semantics to show how it works in these
terms.  I guess it couldn't be done without some hand-waving, due to
non-functional structures like loops, but those are just details.

So here's a tiny Haskell example that reads a line from input and
prints it out with quotes around it:

   module Main (main) where
   main = do
       line <- getLine
       putStrLn (show line)

"main" is (=) a function that can be executed with that result.
This executability is a property of the "IO Monad", and this
function has that type -- main :: IO () -- but that's more than
we need to know for the present purposes.  The point is that
this "do" structure looks like a statement syntax, but it's
strictly equivalent to another more functional notation

   main = getLine >>= (\line -> putStrLn (show line))

Haskell's lambda syntax: \ arg -> expression.  You could read this
aloud as "bind getLine to a lambda function with one parameter ..."
and the result is a functional relation that can be executed.
Each step binds a function to a lamba that forms the entire rest
of the "do" block, not just the next step -- it's a nested sequence,
not a linear one.

So I'm proposing that Python's = operator is like Haskell's "<-",
and "foo = 5" is conceptually like "bind 5 to a lambda with one
argument "foo" implementing the rest of the procedure body."  The
confusion is not because foo needs to be so different, it's that
Python "=" is not at all math "=".

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list