LISP macros vs Ruby procs ?

Discussion in 'Ruby' started by Aldric Giacomoni, Nov 6, 2009.

  1. 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/.
    Aldric Giacomoni, Nov 6, 2009
    #1
    1. Advertising

  2. Aldric Giacomoni

    lith Guest

    > 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
    lith, Nov 6, 2009
    #2
    1. Advertising

  3. Aldric Giacomoni

    Ryan Davis Guest

    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.
    Ryan Davis, Nov 6, 2009
    #3
  4. 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.
    David Masover, Nov 7, 2009
    #4
  5. On Fri, Nov 6, 2009 at 3:05 PM, lith <> 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
    Rick DeNatale, Nov 7, 2009
    #5
  6. Aldric Giacomoni

    lith Guest

    > 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.
    lith, Nov 7, 2009
    #6
  7. 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/.
    Aldric Giacomoni, Nov 7, 2009
    #7
  8. 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

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Nov 7, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kenny Tilton

    Re: What you can do with macros in Lisp

    Kenny Tilton, Oct 12, 2003, in forum: Python
    Replies:
    2
    Views:
    283
    Kenny Tilton
    Oct 12, 2003
  2. ekzept
    Replies:
    0
    Views:
    354
    ekzept
    Aug 10, 2007
  3. nanothermite911fbibustards
    Replies:
    0
    Views:
    362
    nanothermite911fbibustards
    Jun 16, 2010
  4. nanothermite911fbibustards
    Replies:
    0
    Views:
    307
    nanothermite911fbibustards
    Jun 16, 2010
  5. matt
    Replies:
    1
    Views:
    243
    George Ogata
    Aug 6, 2004
Loading...

Share This Page