Adding a new statement to Python(eli.thegreenplace.net)
eli.thegreenplace.net
Adding a new statement to Python
http://eli.thegreenplace.net/2010/06/30/python-internals-adding-a-new-statement-to-python/
15 comments
Very useful post I think. Not to depreciate Python the language, but it might be interesting to compare "adding a new statement to Python" with "adding a new statement to Lisp". Especially for people struggling to grasp Lisp's benefits.
Or Forth.
http://www.yosefk.com/blog/my-history-with-forth-stack-machi...
my mind was immediately blown away by the following passage right at the top of system.fth, the part of pForth implemented in Forth on top of the C interpreter:
http://www.yosefk.com/blog/my-history-with-forth-stack-machi...
my mind was immediately blown away by the following passage right at the top of system.fth, the part of pForth implemented in Forth on top of the C interpreter:
: ( 41 word drop ; immediate
( That was the definition for the comment word. )
( Now we can add comments to what we are doing! )Here, adding a 'unless' statement:
Macro in Lisp are essentially functions taking code as argument and returning code, but they are executed at compile-time.
Edit: replaced with the correct version, sorry for the error.
(defmacro unless [expr & body] `(if ~expr nil (do ~@body)))
Taken from:
http://stackoverflow.com/questions/2977882/defining-clojure-...Macro in Lisp are essentially functions taking code as argument and returning code, but they are executed at compile-time.
Edit: replaced with the correct version, sorry for the error.
According to that question's answer and comments, that definition is buggy (making it not quite a fair comparison). Here is a less buggy, more flexible version, taken from a comment:
(defmacro unless [expr & body] `(if ~expr nil (do ~@body)))Or adding a new statement to Tcl:
http://wiki.tcl.tk/917
http://wiki.tcl.tk/917
Or BASIC ;-) http://en.wikipedia.org/wiki/DOS_Wedge
On a more serious note, the ability to add constructs to the language is not nearly as unique to LISP as Lispers think it is.
On a more serious note, the ability to add constructs to the language is not nearly as unique to LISP as Lispers think it is.
Or Io:
unless := method(call evalArgAt(0) ifFalse(call evalArgAt(1)))
Usage example... unless(1 == 2, writeln("Tis false"))I was reminded of this by the post on the goto statement -- having used the AST module recently in a limited capacity[1], I was really impressed by how easy it was to use. There's a good pycon talk for those interested specifically in this part of the process[2], and you can also see the full grammar[3].
[1] http://blueprintforge.com/blog/2012/02/27/static-modificatio...
[2] http://blip.tv/pycon-us-videos-2009-2010-2011/pycon-2011-wha...
[3] http://docs.python.org/library/ast.html#abstract-grammar
[1] http://blueprintforge.com/blog/2012/02/27/static-modificatio...
[2] http://blip.tv/pycon-us-videos-2009-2010-2011/pycon-2011-wha...
[3] http://docs.python.org/library/ast.html#abstract-grammar
Here's my original paper on the topic in case folks are interested:
http://tomlee.co/wp-content/uploads/2008/12/python-language-...
http://tomlee.co/wp-content/uploads/2008/12/python-language-...
How well do you know the Sutras?
Special cases aren't special enough to break the rules. ... There should be one-- and preferably only one --obvious way to do it.
Special cases aren't special enough to break the rules. ... There should be one-- and preferably only one --obvious way to do it.
Did you even try to read the linked article :) ?
Here's the second section:
A language-advocacy digression
------------------------------
This article doesn’t attempt to suggest the addition of an until statement to Python. Although I think such a statement would make some code clearer, and this article displays how easy it is to add, I completely respect Python’s philosophy of minimalism. All I’m trying to do here, really, is gain some insight into the inner workings of Python.
Here's the second section:
A language-advocacy digression
------------------------------
This article doesn’t attempt to suggest the addition of an until statement to Python. Although I think such a statement would make some code clearer, and this article displays how easy it is to add, I completely respect Python’s philosophy of minimalism. All I’m trying to do here, really, is gain some insight into the inner workings of Python.
point taken
[deleted]