LISP macros vs Ruby procs ?

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

  1. I'm reading the LISP book on 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.


    Aldric Giacomoni, Nov 6, 2009
    1. Advertisements

  2. Aldric Giacomoni

    lith Guest

    LISP's ` (the backtick) is roughly equivalent to Ruby's eval with a
    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.
    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.

    [2] IIRC slib[3] contains
    an implementation for older scheme interpreters.
    lith, Nov 6, 2009
    1. Advertisements

  3. Aldric Giacomoni

    Ryan Davis Guest

    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
  4. New syntax is the main thing.

    For example, take Pure:

    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
  5. 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.

    Rick DeNatale

    Rick DeNatale, Nov 7, 2009
  6. Aldric Giacomoni

    lith Guest

    The enabling feature of Lisp which makes its macros possible is
    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
  7. 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
    Aldric Giacomoni, Nov 7, 2009
  8. Aldric Giacomoni wrote:
    if @thread.closed?

    (couldn't resist!)

    Marnen Laibow-Koser, Nov 7, 2009
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.