Copyright (C) 2009-2010, Trevor Davel <twylite AT crypt DOT co DOT za>
See the file "LICENSE.txt" (Tcl/Tk License) for information on usage and redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
control::functional - Introduction, Motivation and Goals
Introduction & Motivation
Functional programming is becoming more widely used in industry. It has been popularised by high-availability telecoms systems (Ericsson's Erlang) and the application of functional concepts to scalability (e.g. Google's mapreduce), and is spurred on by success stories from large web site (like Facebook).
Tcl does not "have" higher-order functions, but it supports functional concepts in its own way:
foreachis an iterative construct that can do anything
foldris able to; in fact
uplevelcan be used to implement all these higher-order functions.
- The practice of concatenating arguments onto a script and
evalulating it is - to some extent - an anonymous function that lends itself naturally to currying. Any
procthat accepts and
evals such a script is a higher-order function.
- The Tclers Wiki is full of examples that demonstrate the functional paradigm in Tcl, including a plethora of code snippets that look like map, reduce, filter, etc. but are limited in some way.
There are also extensions that provide well-known higher-order functions such as those mentioned above: Tcllib's struct::list, Wub's functional-1.0.tm, and NEM's More functional programming (aka 'funtcl') are the most widely known.
Still, Tcl does not "have" higher-order functions, in the same way that Tcl 8.4 did not "have" OO. And Tcl needs higher-order functions for much the same reasons that it needed OO:
- A common base system to promote reuse.
- Prevent authors from reinventing the wheel (poorly).
- Stop ill-informed criticism that Tcl doesn't support functional programming (or at least higher-order functions).
- Performance gains can be made by C and bytecoded implementations.
Enhancements to Tcl's support for functional programming should probably meet the following goals:
- Tcl's support for higher-order functions should feel familiar (tclish and functional) and be directly comparable with other languages.
- The API should define an intuitive and terse syntax that is visually accessible, plays nicely with the various things in Tcl that look like 'functions', and handles common variants of higher-order functions (for exmaple folds with and without initial value, variadic map).
- There are many 'function-like things' in Tcl: named and anonymous procs, objects, ensembles, expressions, etc. We should not be limited unreasonably to using a subset of these 'function-like things' with higher-order functions.
- Fully specify the behaviour of each function with regard to context of execution, list processing order, interaction with break/continue, error propagation, effect of modifying the input set during the operation, etc. We should get the semantics right, not document them as a side-effect of the chosen implementation.
- We are aiming for semantic correctness, not performance of the pure-Tcl implementation.
- First deliverable is a pure-Tcl implementation and test suite. This will provide usable code that the community can test, experiment with, and break (so that the model and/or implementation may be corrected before it becomes a bytecoded bug).