Subject: Lambda in Tcl - DN [1]
"Richard.Suchenwirth" <Richard.Suchenwirth@kst.siemens.de> - 05 Jul 1999 - comp.lang.tcl
Spent some time this weekend looking at other languages (Python, Lisp):
- what do they have which Tcl hasn't?
- is it worth having?
- how can we do it in Tcl?
One of those thing is the anonymous function, or Lambda. Think of it
this way: the three attributes of a proc are its name, its args, and its
body. A lambda expression only has args and body and evaluates to a
thingy that can be used like a proc's name. Concrete example:
You have a list of data (e.g. prices) and want to make a list of results
where some treatment was done to each element, e.g. add 16% VAT.
Straightforward:
set L {10 20 30 40 50}
set res {}
foreach i $L {lappend res [expr $i*1.16]}
set L2 $res
Now if you want to wrap the single-element processing and the list
processing in separate procs, you might say:
proc addVAT {x} {expr $x*1.16}
proc map {fun list} {
set res {}
foreach i $list {lappend res [$fun $i]}
return $res
}
set L2 [map addVAT $L]
Now if you don't want to invent a proc name for each single-element
proc, you're ripe for Lambda: given an arglist and a body, add a name,
and make it a proc. One proposal: lambdas are anonymous - no name -
empty string (which is a valid proc name in Tcl), so:
proc lambda {argl body} {
set name {}
proc $name $argl $body
return $name
}
set L2 [map [lambda x {expr $x*1.16}] $L]
This is how they do it in Lisp or Python. This implementation has the
feature that always the same name is used, so nested lambdas wouldn't
work (but I couldn't imagine an application for that, either...)
If you want nonconflicting names for the lambda object, you might use
"very telling names": use the line
set name $argl/$body
which completely describes the function being defined. Luckily, the body
is often very short in lambdas...;-)
On the other hand, in order to define a "proc with no name", or rather
the empty string as name, we need not even define a lambda proc as
above. Tcl's proc command returns nothing, which is the empty string,
which happens to be the return value of lambda in the first alternative.
So
we may say
set L2 [map [proc "" x {expr $x*1.16}] $L]
So if anyone slightens Tcl for not having a Lambda function, tell them
it's called proc "" and was there all the time (just we didn't think of
it...;-)
And then: Of course we can model all the functional programming goodies
in Python, but there may be cooler ways:
To sum the numbers in a list, you may
set sum [reduce [lambda {p q} {expr $p+$q}] 0] $L
(that's how it's done in Python, translated to Tcl, but:)
set sum [expr [join $L +]+0]
does the same job and looks definitely slicker...
--
Schoene Gruesse/best regards, Richard Suchenwirth -- tel. +49-7531-86
2703
> RC DT2, Siemens Electrocom GmbH, Buecklestr. 1-5, D-78467 Konstanz, Germany
> My opinions were not necessarily, or will not necessarily be, mine.
Last modified
1999-09-27
1999-09-27
(195.108.246.50)
Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated
