I'm reading the LISP book on www.gigamonkeys.com to take a look at what
some people still call the best language ever. It's pretty cool.
I'm reading about macros now, and I remember from Ola Bini's blog that
Ruby can't do macros.
In my limited understanding, it seems that the only thing separating
Ruby from "true macros" is the ability to ... Define new keywords / new
syntax ?
LISP's ` (the backtick) is roughly equivalent to Ruby's eval with a
string, right?
If there are any seasoned LISPers here, can you think of a sample LISP
macro that can't be written in Ruby, to help me understand better?
LISP's ` (the backtick) is roughly equivalent to Ruby's eval with a
string, right?
That like saying a string is equivalent to an array/list. In lisp, a
macro can take the arguments transform it using the usual set of list-
related functions and generate the code for the macro. In scheme, you
can use some sort of structured pattern matching to generate the code.
If there are any seasoned LISPers here, can you think of a sample LISP
macro that can't be written in Ruby, to help me understand better?
I haven't written a single line in lisp for years but: Try to
implement lisp's loop form[1] or scheme's hygienic macros[2] in ruby
-- you're allowed to use only ruby 1.9 standard library.
yes and no... one of the other differences is that lisp macros are handled at parse time, not runtime.
You might want to look at caleb's ruby macros... I could never get it to work for me, but you may have better luck.
···
On Nov 6, 2009, at 11:28 , Aldric Giacomoni wrote:
In my limited understanding, it seems that the only thing separating
Ruby from "true macros" is the ability to ... Define new keywords / new
syntax ?
LISP's ` (the backtick) is roughly equivalent to Ruby's eval with a
string, right?
I was slow to grasp this point, but this actually cannot be written with
traditional blocks -- at least, not easily. The implementation actually parses
the source as a string.
I'm pretty sure Lisp could do something like this as a macro.
···
On Friday 06 November 2009 01:28:49 pm Aldric Giacomoni wrote:
In my limited understanding, it seems that the only thing separating
Ruby from "true macros" is the ability to ... Define new keywords / new
syntax ?
The enabling feature of Lisp which makes its macros possible is
homoiconicity, which means that the external representation of a lisp
program is exactly a human-readable form of the internal executable
representation, plus the fact that this internal representation is a
fundamental data-type of the language.
This means that Lisp code can manipulate and generate the internal
executable program representation directly and macros are really just
Lisp procedures which do just that.
On Fri, Nov 6, 2009 at 3:05 PM, lith <minilith@gmail.com> wrote:
LISP's ` (the backtick) is roughly equivalent to Ruby's eval with a
string, right?
That like saying a string is equivalent to an array/list. In lisp, a
macro can take the arguments transform it using the usual set of list-
related functions and generate the code for the macro. In scheme, you
can use some sort of structured pattern matching to generate the code.
The enabling feature of Lisp which makes its macros possible is
homoiconicity
It makes things easier and more elegant. Basically, you could also use
a parser of sorts (ruby 1.9 comes with ripper), make your
transformations, and then generate a string or sexp that is then
evaluated. One could use e.g. the polyglot gem to let source files be
read by such a macro pre-processor. In comparison to the lisp approach
that would be terribly complicated and fragile though.
I know of 2-3 efforts to implement macros in ruby. Maybe the OP would
be interested in those approaches.
It could also be interesting to compare lisp macros with template
haskell, camlp5 and similar approaches that, if I'm not mistaken, seem
to solve similar problems.
The enabling feature of Lisp which makes its macros possible is
homoiconicity
It makes things easier and more elegant. Basically, you could also use
a parser of sorts (ruby 1.9 comes with ripper), make your
transformations, and then generate a string or sexp that is then
evaluated. One could use e.g. the polyglot gem to let source files be
read by such a macro pre-processor. In comparison to the lisp approach
that would be terribly complicated and fragile though.
I know of 2-3 efforts to implement macros in ruby. Maybe the OP would
be interested in those approaches.
Thanks, everyone for the responses. I am, at the moment, merely a Ruby
enthusiast who is seriously considering the conversion to full-fledged
Rubyist.. So, I'm doing my homework
I don't currently deal with problems complicated enough to warrant the
implementation of macros. As things stand, I am just now beginning to
grasp the implications for Procs and Lambda, and when they get used..
This is all fascinating though. I am, for instance, particularly
enjoying the ++ thread, which now looks like it's pretty much come to a
close.