The S combinator is a sort-of generalised function application in Combinatory Logic, the other most basic one being K.

proc S {f g x} {
    $f $x [$g $x]

It should however be observed that the evaluation model of combinator theory is quite different from that of Tcl — in particular it repeats evaluation as many times as possible, quite contrary to rule 11 of the dodekalogue which states that there (on the syntactic level) is exactly one round of evaluation in Tcl (commands such as eval may perform another, but that is on a semantic level) — and hence this S proc is not as general as the S combinator. See Hot curry or Combinator engine for more on combinators.

Larry Smith Would this not then be a better implementation?

 proc S { f g x } {
   set oldcmd ""
   set cmd "$f $g $x"
   while {$oldcmd ne $cmd} {
     set oldcmd $cmd; set cmd [ eval $cmd ]
   # presumeably
   return $cmd

[Can someone provide some context here on this page as to how this would be used in the real world?]

RS: It certainly is less evidently usable than K, but rather of theoretical interest (like e.g. particle physics) - one of the smallest building blocks of functions. You can for example compose identity out of S and two instances of K:

I = S K K

but in the "real Tcl world" one would of course code identity (which is useful at times) as

proc I x {set x}

PT writes: Note that to do the above you need to define K in such a manner that it has optional arguments. To actually do this in Tcl we can do:

proc K {a args} {set a}
proc S {a b c} {$a $c [$b $c]}
proc I {a} {S K K $a}

DKF:Actually, you need to run with the sort of magic listed in Hot curry or Combinator engine. There's more to it than just handling more arguments and this gets very deep very quickly. It's been explained better elsewhere, so I won't say more here.