Live data from Hacker News

The Nature of Lisp

defmacro.org

11–20 of 82 posts

Re: The Nature of Lisp

#11
post #3

To understand Lisp is to understand interpreters. With that understanding you can create domain specific languages which is extremely powerful. But I wouldn't recommend using Lisp itself.. macros in particular are unhygienic.

You can do cool stuff with unhygienic macros, however, like anaphoric macros. Interested readers should check out On Lisp by Paul Graham, as well as Let Over Lambda by Doug Hoyte.

You can still do anaphoric macros with hygienic macros.

http://blog.racket-lang.org/2008/02/dirty-looking-hygiene.ht...

And http://blog.racket-lang.org/2011/04/writing-syntax-case-macr...

Re: The Nature of Lisp

#12
post #4
post #2

I found that a rather good introduction to code as data, but I am not sure whether I am supposed to have been hit by the enlightenment he describes… :-)

Yes, for me the first time I read about the lisp syntax I was thinking: "oh cool, it makes (+ 2 2) exactly equivalent to the syntaxic tree + / \ 2 2 " But I don't find it particularly enlightening and I still don't see what cool stuff you can do with macros that you can't do elsewhere.

Macros extend the power of the language way beyond its core primitives. For instance, I wrote a macro, TEMPORARY-ASSIGN. I use it like this:

  (TEMPORARY-ASSIGN ((traversing obj) true)
     ... do stuff ...)
In Python, the equivalent code would be.

   old_trav = obj.traversing
   obj.traversing = True
   try:
      ... do stuff...
   finally:
      obj.traversing = old_trav
There's no way to abstract out that pattern in Python. Every time you want to temporarily assign a field or variable, you're stuck writing the above code. Another example:

   (defun foo (x y) ...)
is how you define a function in Common Lisp. I wrote a macro, DEFUN-CACHE

   (defun-cache foo (x y) ...)
which is the cached version. In Python, you can do the same with decorators, but that's one more tacked-on feature. Lisp programmers have been writing defun-cache since 40 years.

If you want to learn more, Paul Graham's On Lisp is the definitive book on the topic. You can download it for free http://www.paulgraham.com/onlisp.html, and it's very readable, even if you're not a Lisper.

Re: The Nature of Lisp

#13
post #12
post #4

Earlier quoted context omitted.

Yes, for me the first time I read about the lisp syntax I was thinking: "oh cool, it makes (+ 2 2) exactly equivalent to the syntaxic tree + / \ 2 2 " But I don't find it particularly enlightening and I still don't see what cool stuff you can do with macros that you can't do elsewhere.

Macros extend the power of the language way beyond its core primitives. For instance, I wrote a macro, TEMPORARY-ASSIGN. I use it like this: (TEMPORARY-ASSIGN ((traversing obj) true) ... do stuff ...) In Python, the equivalent code would be. old_trav = obj.traversing obj.traversing = True try: ... do stuff... finally: obj.traversing = old_trav There's no way to abstract out that pattern in Python. Every time you want…

> There's no way to abstract out that pattern in Python.

I'm sure there are macros that can't be abstracted out in Python but this isn't one of them:

    from contextlib import contextmanager
    
    @contextmanager
    def temp_assign(obj, attr, val):
        old_val = getattr(obj, attr)
        setattr(obj, attr, val)
        yield
        setattr(obj, attr, old_val)
    
    class X:
        pass
    
    x = X()
    x.a = 1
    with temp_assign(x, "a", 2):
       print x.a # prints 2
    print x.a # prints 1

Re: The Nature of Lisp

#14
post #12
post #4

Earlier quoted context omitted.

Yes, for me the first time I read about the lisp syntax I was thinking: "oh cool, it makes (+ 2 2) exactly equivalent to the syntaxic tree + / \ 2 2 " But I don't find it particularly enlightening and I still don't see what cool stuff you can do with macros that you can't do elsewhere.

Macros extend the power of the language way beyond its core primitives. For instance, I wrote a macro, TEMPORARY-ASSIGN. I use it like this: (TEMPORARY-ASSIGN ((traversing obj) true) ... do stuff ...) In Python, the equivalent code would be. old_trav = obj.traversing obj.traversing = True try: ... do stuff... finally: obj.traversing = old_trav There's no way to abstract out that pattern in Python. Every time you want…

I think I don't understand what your macro does, because the Python code is easily abstractable with a context manager:

  from contextlib import contextmanager

  @contextmanager
  def traverse(obj, attr, val):
    cached = getattr(obj, attr)
    setattr(obj, attr, val)
    yield
    setattr(obj, attr, cached)

  with traverse(obj, "traversing", True):
    # ... do stuff ...
Could you explain what your example does that I've missed?

Re: The Nature of Lisp

#16
post #12

Earlier quoted context omitted.

Macros extend the power of the language way beyond its core primitives. For instance, I wrote a macro, TEMPORARY-ASSIGN. I use it like this: (TEMPORARY-ASSIGN ((traversing obj) true) ... do stuff ...) In Python, the equivalent code would be. old_trav = obj.traversing obj.traversing = True try: ... do stuff... finally: obj.traversing = old_trav There's no way to abstract out that pattern in Python. Every time you want…

> There's no way to abstract out that pattern in Python. I'm sure there are macros that can't be abstracted out in Python but this isn't one of them: from contextlib import contextmanager @contextmanager def temp_assign(obj, attr, val): old_val = getattr(obj, attr) setattr(obj, attr, val) yield setattr(obj, attr, old_val) class X: pass x = X() x.a = 1 with temp_assign(x, "a", 2): print x.a # prints 2 print x.a # prin…

I think it's an extraordinary strength of Python that I hadn't seen your code when writing mine but that other than two variable names they're identical. Leaving my comment up for demonstration of this.

Re: The Nature of Lisp

#17
post #6
post #3

To understand Lisp is to understand interpreters. With that understanding you can create domain specific languages which is extremely powerful. But I wouldn't recommend using Lisp itself.. macros in particular are unhygienic.

I am admittedly still a Lisp (et. al.) rookie, but isn't the entire point of Scheme that it introduces hygienic macros? Or are you referring to some other (perhaps sarcastic) notion of macro hygiene?

That's one design characteristic of Scheme, but I wouldn't call it "the entire point".

Re: The Nature of Lisp

#18

Earlier quoted context omitted.

> There's no way to abstract out that pattern in Python. I'm sure there are macros that can't be abstracted out in Python but this isn't one of them: from contextlib import contextmanager @contextmanager def temp_assign(obj, attr, val): old_val = getattr(obj, attr) setattr(obj, attr, val) yield setattr(obj, attr, old_val) class X: pass x = X() x.a = 1 with temp_assign(x, "a", 2): print x.a # prints 2 print x.a # prin…

I think it's an extraordinary strength of Python that I hadn't seen your code when writing mine but that other than two variable names they're identical . Leaving my comment up for demonstration of this.

Ha, awesome! I went for an exact transliteration although if I were to use this idea for real I would probably do the assignment explicitly in the body. I think this looks a bit more pythonic:

    @contextmanager
    def restoring(obj, attr):
        old_val = getattr(obj, attr)
        yield
        setattr(obj, attr, old_val)
    
    x.a = 1    
    with restoring(x, "a"):
       print x.a
       x.a = 2
       print x.a
    print x.a

Re: The Nature of Lisp

#19
post #9

Very good article, though I doubt it'll convince the usual mass of unbelievers. (I love Lisp, for the record, though my primary exposure has been through Emacs Lisp - so shoot me). A really great book that helps you get appreciate the concepts in Lisp, without really talking about Lisp directly too much, is "Patterns of Software" by Peter Gabriel. http://amzn.to/TxDKGG I found it to be a very enlightening read. Defin…

Richard Gabriel. Though Peter Gabriel would be good too :)

Re: The Nature of Lisp

#20
post #9

Very good article, though I doubt it'll convince the usual mass of unbelievers. (I love Lisp, for the record, though my primary exposure has been through Emacs Lisp - so shoot me). A really great book that helps you get appreciate the concepts in Lisp, without really talking about Lisp directly too much, is "Patterns of Software" by Peter Gabriel. http://amzn.to/TxDKGG I found it to be a very enlightening read. Defin…

And a good book for folks who already know how to program and want to learn Lisp in some depth is "Practical Common Lisp" by Peter Seibel. http://www.gigamonkeys.com/book/
Post reply on HN