[Python-ideas] allow `lambda' to be spelled λ
Stephan Houben
stephanh42 at gmail.com
Tue Jul 12 08:38:22 EDT 2016
Hi list,
Here is my speculative language idea for Python:
Allow the following alternative spelling of the keyword `lambda':
λ
(That is "Unicode Character 'GREEK SMALL LETTER LAMDA' (U+03BB).")
Background:
I have been using the Vim "conceal" functionality with a rule which visually
replaces lambda with λ when editing Python files. I find this a great
improvement in
readability since λ is visually less distracting while still quite
distinctive.
(The fact that λ is syntax-colored as a keyword also helps with this.)
However, at the moment the nice syntax is lost when looking at the file
through another editor or viewer.
Therefore I would really like this to be an official part of the Python
syntax.
I know people have been clamoring for shorter lambda-syntax in the past, I
think this is
a nice minimal extension.
Example code:
lst.sort(key=lambda x: x.lookup_first_name())
lst.sort(key=λ x: x.lookup_first_name())
# Church numerals
zero = λ f: λ x: x
one = λ f: λ x: f(x)
two = λ f: λ x: f(f(x))
(Yes, Python is my favorite Scheme dialect. Why did you ask?)
Note that a number of other languages already allow this. (Racket, Haskell).
You can judge the aesthetics of this on your own code with the following
sed command.
sed 's/\<lambda\>/λ/g'
Advantages:
* The lambda keyword is quite long and distracts from the "meat" of the
lambda expression.
Replacing it by a single-character keyword improves readability.
* The resulting code resembles more closely mathematical notation (in
particular, lambda-calculus notation),
so it brings Python closer to being "executable pseudo-code".
* The alternative spelling λ/lambda is quite intuitive (at least to anybody
who knows Greek letters.)
Disadvantages:
For your convenience already noticed here:
* Introducing λ is introducing TIMTOWTDI.
* Hard to type with certain editors.
But note that the old syntax is still available.
Easy to fix by upgrading to VIM ;-)
* Will turn a pre-existing legal identifier λ into a keyword.
So backward-incompatible.
Needless to say, my personal opinion is that the advantages outweigh the
disadvantages. ;-)
Greetings,
Stephan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160712/6363bc8a/attachment.html>
More information about the Python-ideas
mailing list