Version 12 of s

Updated 2007-11-30 17:23:46 by LV

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

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

See Hot curry or Combinator engine for much more.

[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.