Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > LISP macros vs Ruby procs ?

Reply
Thread Tools

LISP macros vs Ruby procs ?

 
 
Aldric Giacomoni
Guest
Posts: n/a
 
      11-06-2009
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?

Your help will be much appreciated.

Thanks,

--Aldric
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
lith
Guest
Posts: n/a
 
      11-06-2009
> 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/documentati...ody/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

 
Reply With Quote
 
 
 
 
Ryan Davis
Guest
Posts: n/a
 
      11-06-2009

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?


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.


 
Reply With Quote
 
David Masover
Guest
Posts: n/a
 
      11-07-2009
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 ?


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.

 
Reply With Quote
 
Rick DeNatale
Guest
Posts: n/a
 
      11-07-2009
On Fri, Nov 6, 2009 at 3:05 PM, lith <(E-Mail Removed)> 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/pers...-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

 
Reply With Quote
 
lith
Guest
Posts: n/a
 
      11-07-2009
> 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.

 
Reply With Quote
 
Aldric Giacomoni
Guest
Posts: n/a
 
      11-07-2009
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
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.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Marnen Laibow-Koser
Guest
Posts: n/a
 
      11-07-2009
Aldric Giacomoni 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nice historical Musical - VERY RELAXING - about LISP history -fundamental ideas of LISP nanothermite911fbibustards C++ 0 06-16-2010 09:47 PM
Nice historical Musical - VERY RELAXING - about LISP history -fundamental ideas of LISP nanothermite911fbibustards Python 0 06-16-2010 09:47 PM
pat-match.lisp or extend-match.lisp in Python? ekzept Python 0 08-10-2007 06:08 PM
procs/blocks - blocks with procs, blocks with blocks? matt Ruby 1 08-06-2004 01:33 AM
Re: What you can do with macros in Lisp Kenny Tilton Python 2 10-12-2003 09:50 PM



Advertisments