If we had no variables

Richard Suchenwirth 2006-01-25 - Variables are popular parts of many programming languages, and most coders have no resistance to write something like

   i = i+1

which would cause mathematicians to frown.

In functional programming, variables are less popular. In Functional Programming (Backus 1977) it was even tried to do almost totally without them, by just composing functions instead of data objects. Forth and similar RPN systems also mostly do without variables: there, the stack is the big thing that varies all the time :^)

Here's experiments with a Tcl in which we don't have variables in the sense that they can vary. To demonstrate that, let's first of all get rid of the set command:

 rename set {}

(Seriously, almost all the commands which cause side-effects by changing values, as discussed in Dangers of creative writing, should be discarded. But this is just experimental...

We still can associate names with values, by using argument-less procs:

 proc pi {} {return 3.141}

and due to Tcl's dynamics, we still can change such associations too

 proc pi {} {return 3}

but let's resolve we won't. Procs as constants are also global, so we don't need to import them into function bodies.

For nicer looks (the idea was borrowed from Scheme), let's also have just one unified definer which handles both argument-less and argumented functions:

 proc def args {
     if {[lindex $args 1] eq "="} {
         proc [lindex $args 0] {} [list return [lindex $args 2]]
     } else {
         proc [lindex $args 0] [lindex $args 1] [lindex $args 3]

What we also still have is local variables of functions. We can read their value with the $name notation, but we can't vary their value any more (if we stick to our resolution). Let's rather call them "parameters".

Now experimenting with this once again different flavor of Tcl :-)

 def a = 6
 def b = 7
 def mul {a b} = {expr {$a * $b}}

 puts [mul [a] [b]]
 puts [mul 111 6]

See also If we had no if - If we had no proc

Arts and crafts of Tcl-Tk programming