Version 11 of Erlang Idioms for Tcl

Updated 2003-10-03 17:38:27

Erlang is very cool. It's functional (declaritive too), supports massive concurency and promotes a number of programming idioms I find myself applying to Tcl. You don't have to emulate it via syntatical FP sugar (Lambda in Tcl, List Comprehension, etc). The idioms are rich and Tcl is up to the task! Tcl (with Threads) can do COP too ;-)

My network servers (coded in Tcl) are starting to model the way Erlang/OTP does things.

(Some of the items below are FP idioms, not just Erlang)

Some of the simple idioms:

  • Minimize side effects -- Try writing Tcl apps without global or namespace variables. Pass state as proc parameters (localize your errors). E.g. Rewrite fileevent/after handlers to accumulate state...
  • Concurrency interaction through message passing -- The Tcl thread package does this beautifully.
  • Controllers/Monitors -- Layer your app with processes/threads that do logic, monitor the threads that do the logic, manage the threads that monitor the threads that do the logic ... etc
  • Handle errors in a higher layer -- Interesting... Don't waste valuable processing time wrapping catch { } around everything. Handle it in a separate (controlling) thread that is signaled with the error.
  • Threads/processes should do only one thing, etc.
  • Message Receivers in Erlang: Queue messages received and use pattern matching to select the ones you are interested in.

Some things you can't really do well (or at all) in Tcl:

  • Guarded Function Clauses
    Could you elaborate upon this, please?

If one had a copious amount of time, one could adapt Erlang/OTP to Tcl. That would be very nice...

-- Todd Coram

Although I have seen variations upon this elsewhere (Tail call optimization), this continues to amuse me: Factorial Using Event Loop.


Erlang makes extensive use of dictionaries (like Tcl arrays). It has a fast and scalable implementation but Scott Lystig Fritchie discovered a way to incorporate something called Judy Arrays into Erlang. His paper http://www.snookles.com/scott/publications/pli2003-slf.pdf compares different implementations of hashes for Erlang.

-- Todd Coram