LISP macros vs Ruby procs?

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 B.'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?

Your help will be much appreciated.

Thanks,

–Aldric

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.

[1] http://www.lispworks.com/documentation/HyperSpec/Body/m_loop.htm
[2] http://en.wikipedia.org/wiki/Hygienic_macro IIRC slib[3] contains
an implementation for older scheme interpreters.
[3] http://people.csail.mit.edu/jaffer/slib_toc.html

On Nov 6, 2009, at 11:28 , Aldric G. 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?

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 Friday 06 November 2009 01:28:49 pm Aldric G. 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 ?

New syntax is the main thing.

For example, take Pure:

http://purefunctional.rubyforge.org/

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 Fri, Nov 6, 2009 at 3:05 PM, lith [email protected] 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, 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.

http://en.wikipedia.org/wiki/Homoiconicity
http://c2.com/cgi/wiki?DefinitionOfHomoiconic


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

lith wrote:

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 :slight_smile:
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.

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.

Aldric G. wrote:
[…]

I am, for instance, particularly
enjoying the ++ thread, which now looks like it’s pretty much come to a
close.

if @thread.closed?
@thread.posts.count++
end

(couldn’t resist!)

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]