Why Lisp macros are cool, a Perl perspective (2005)
lists.warhead.org.uk
lists.warhead.org.uk
I forgot what rabid programming language fans are like; they can and will defend any system, no matter how dysfunctional, as long as it's _their_ dysfunctional sytem.
:-)
(defmacro set-sqrt (place v) `(setf ,place (* v v)))
What does the comma right before place in its second usage signify?Also, am I right in assuming that setf turns:
(setf (f1 arg1) arg2)
into (setter-for-f1 arg1 arg2)(Ironically, MJD made one of the same mistakes there he complained about in his C examples -- mentioning 'v' twice, which makes any side effects happen twice. Lisp macros are better than C's, but still require some care.)
defsetf turns that expression into whatever the defsetf expander for f1 returns -- it's up to the code.
Read http://www.gigamonkeys.com/book/macros-defining-your-own.htm... for a more thorough explanation.
As for setf, a user can create arbitrary 'setters' using syntax like that described at http://gigamonkeys.com/book/object-reorientation-classes.htm...
(setf (f1 arg1) arg2)
will cause an error, but (setf (sqrt arg1) arg2)
will expand into (setf arg1 (* v v))
which, as you can see, is nonsensical in terms of what it looks like he was trying to do. (defmacro set-sqrt (place v) `(setf ,place (* v v)))
should actually be (defun square (x) (* x x))
(defmacro set-sqrt (place v) `(setf ,place (square ,v)))It "punches a hole" in the quote: the ` in `(setf ,place (* v v)) says "don't evaluate this, this is a litteral value/list", but then the comma indicates "actually evaluate just this expression right here within the definition context" so ,place will be replaced by the value of the place argument. It's kinda equivalent to #{} in Ruby strings.
As others have pointed out, v should probably be unquoted as well.
def my_gimme(thing)
gimme "select #{thing} from something"
end
Of course, in lisp, the "interpolation" (and macros in general) is structural: you can't use malformed strings to inject unexpected code like you could in my silly example.Incidentally, Stuart Halloway uses this same Ruby syntax as pseudo-code when he first introduces macro syntax in his Programming Clojure book.
In short, macros are cool because, instead of hacking the compiler and groveling through its weird data structures to add language features, you can add language features in the language itself, because the language is already traditionally expressed in parse trees. Phrased this way, I don't see why this is a life-changing revelation.
In C++, you already have assignment. You don't need to roll your own.