From: Adrian Kubala
Subject: nested quasiquote?
Date: 
Message-ID: <slrndkdh1q.93e.adrian-news@sixfingeredman.net>
So I was playing with the following definition of once-only, and am
mystified by the use of ``(,,g ,,n):

(defmacro once-only ((&rest names) &body body)
  (let ((gensyms (loop for n in names collect (gensym))))
    `(let (,@(loop for g in gensyms for n in names collect `(,g (gensym ,(symbol-name n)))))
      `(let (,,@(loop for g in gensyms for n in names collect ``(,,g ,,n)))
        ,(let (,@(loop for n in names for g in gensyms collect `(,n ,g)))
           ,@body)))))

Would somebody be kind enough to clarify why this works differently
than, for example, `(,g ,n) or ```(,,,g ,,,n)? Does anybody know a good
tutorial that discusses the subtleties of nested-quasiquote and/or
macro-writing macros?

From: Pascal Bourguignon
Subject: Re: nested quasiquote?
Date: 
Message-ID: <878xx5yvsi.fsf@thalassa.informatimago.com>
Adrian Kubala <···········@sixfingeredman.net> writes:

> So I was playing with the following definition of once-only, and am
> mystified by the use of ``(,,g ,,n):
>
> (defmacro once-only ((&rest names) &body body)
>   (let ((gensyms (loop for n in names collect (gensym))))
>     `(let (,@(loop for g in gensyms for n in names collect `(,g (gensym ,(symbol-name n)))))
>       `(let (,,@(loop for g in gensyms for n in names collect ``(,,g ,,n)))
>         ,(let (,@(loop for n in names for g in gensyms collect `(,n ,g)))
>            ,@body)))))
>
> Would somebody be kind enough to clarify why this works differently
> than, for example, `(,g ,n) or ```(,,,g ,,,n)? Does anybody know a good
> tutorial that discusses the subtleties of nested-quasiquote and/or
> macro-writing macros?

Use the REPL, Luke!

[55]> (defparameter g '(+ 1 2))
G
[56]> (defparameter n '(* 3 4))
N
[57]> `(,g ,n)
((+ 1 2) (* 3 4))
[58]> ``(,,g ,,n)
(LIST (+ 1 2) (* 3 4))
[59]> 

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay
From: Emre Sevinc
Subject: Re: nested quasiquote?
Date: 
Message-ID: <87fyrd3vnq.fsf@ileriseviye.org>
Pascal Bourguignon <····@mouse-potato.com> writes:

> Adrian Kubala <···········@sixfingeredman.net> writes:
>
>> So I was playing with the following definition of once-only, and am
>> mystified by the use of ``(,,g ,,n):
>>
>> (defmacro once-only ((&rest names) &body body)
>>   (let ((gensyms (loop for n in names collect (gensym))))
>>     `(let (,@(loop for g in gensyms for n in names collect `(,g (gensym ,(symbol-name n)))))
>>       `(let (,,@(loop for g in gensyms for n in names collect ``(,,g ,,n)))
>>         ,(let (,@(loop for n in names for g in gensyms collect `(,n ,g)))
>>            ,@body)))))
>>
>> Would somebody be kind enough to clarify why this works differently
>> than, for example, `(,g ,n) or ```(,,,g ,,,n)? Does anybody know a good
>> tutorial that discusses the subtleties of nested-quasiquote and/or
>> macro-writing macros?
>
> Use the REPL, Luke!
>
> [55]> (defparameter g '(+ 1 2))
> G
> [56]> (defparameter n '(* 3 4))
> N
> [57]> `(,g ,n)
> ((+ 1 2) (* 3 4))
> [58]> ``(,,g ,,n)
> (LIST (+ 1 2) (* 3 4))
> [59]> 

I fired up my SBCL and saw these:

CL-USER> (defparameter g '(+ 1 2)) 
G
CL-USER> (defparameter n '(* 3 4))
N


CL-USER> `(,g ,n)
((+ 1 2) (* 3 4))
CL-USER> ``(,,g ,,n)
`(,(+ 1 2) ,(* 3 4))




-- 
Emre Sevinc

eMBA Software Developer         Actively engaged in:
http:www.bilgi.edu.tr           http://ileriseviye.org
http://www.bilgi.edu.tr         http://fazlamesai.net
Cognitive Science Student       http://cazci.com
http://www.cogsci.boun.edu.tr
From: Emre Sevinc
Subject: Re: nested quasiquote?
Date: 
Message-ID: <878xx53uiq.fsf@ileriseviye.org>
Emre Sevinc <·····@bilgi.edu.tr> writes:

> Pascal Bourguignon <····@mouse-potato.com> writes:
>
>> Adrian Kubala <···········@sixfingeredman.net> writes:
>>
>>> So I was playing with the following definition of once-only, and am
>>> mystified by the use of ``(,,g ,,n):
>>>
>>> (defmacro once-only ((&rest names) &body body)
>>>   (let ((gensyms (loop for n in names collect (gensym))))
>>>     `(let (,@(loop for g in gensyms for n in names collect `(,g (gensym ,(symbol-name n)))))
>>>       `(let (,,@(loop for g in gensyms for n in names collect ``(,,g ,,n)))
>>>         ,(let (,@(loop for n in names for g in gensyms collect `(,n ,g)))
>>>            ,@body)))))
>>>
>>> Would somebody be kind enough to clarify why this works differently
>>> than, for example, `(,g ,n) or ```(,,,g ,,,n)? Does anybody know a good
>>> tutorial that discusses the subtleties of nested-quasiquote and/or
>>> macro-writing macros?
>>
>> Use the REPL, Luke!
>>
>> [55]> (defparameter g '(+ 1 2))
>> G
>> [56]> (defparameter n '(* 3 4))
>> N
>> [57]> `(,g ,n)
>> ((+ 1 2) (* 3 4))
>> [58]> ``(,,g ,,n)
>> (LIST (+ 1 2) (* 3 4))
>> [59]> 
>
> I fired up my SBCL and saw these:
>
> CL-USER> (defparameter g '(+ 1 2)) 
> G
> CL-USER> (defparameter n '(* 3 4))
> N
>
>
> CL-USER> `(,g ,n)
> ((+ 1 2) (* 3 4))
> CL-USER> ``(,,g ,,n)
> `(,(+ 1 2) ,(* 3 4))


An interesting point:

It's generally easy to understand something like `,g
because we can read it:

- OK, Mr. Lisp Reader, you saw a ` hmm, switch to non-eval
mode.
- ... read... read...
- Hmm, here's a comma! ok then switch to eval mode, evaluate
it.

And this loop goes on and on, not much difficulty for understanding.

Now, we (I mean the Lisp newbie) try to read this
using analogy:

  ``,,g

- OK reader you have seen ` which means switch to non-eval
mode, just don't evaluate it until you see a comma
- Whoops! Another ` character... what does that mean? Do not
eval it? Take it as it is? Do not evaluate the "do not evaluate"?
Or simply, take it as it is?
- Aha here we have a comma which means it must be evaluated.
What was g? It was '(+ 1 2) then ,g is (+ 1 2) but we have
another comma before g! Evaluate it again? 
- Or is it something like since there was backquote right
before comma... hmm, things are getting confusing and messy
for the beginner.

Using REPL tells you ```,,,g is same as ``,,g which is
same as `,g so you (as the lisp newbie) start to think
"hmm, maybe it is that ` cancels , and they go one by
one and in the end you have simple `,g left". Is that
right? 

Now, what is the best way to read ```,,,g using natural
language (e.g. English)? 

As far as I can see ``,,g creates problems while 
`,g is very easy to understand. 

Or should we read it that way:

```,,,g is

Do not evaluate and thus have: ``,,,g

which is:

`,,,g

no? 

OK, then there must be a good way and clear way
to read it. All of this may seem stupid to experienced
Lispers or maybe mastering macros and backquoting
is really not that easy.

Maybe a step by step explanation of how ```,,,g
is read by Lisp reader can be good for starters.


-- 
Emre Sevinc

eMBA Software Developer         Actively engaged in:
http:www.bilgi.edu.tr           http://ileriseviye.org
http://www.bilgi.edu.tr         http://fazlamesai.net
Cognitive Science Student       http://cazci.com
http://www.cogsci.boun.edu.tr
From: Barry Margolin
Subject: Re: nested quasiquote?
Date: 
Message-ID: <barmar-318E58.22421407102005@comcast.dca.giganews.com>
In article <··············@ileriseviye.org>,
 Emre Sevinc <·····@bilgi.edu.tr> wrote:

> Maybe a step by step explanation of how ```,,,g
> is read by Lisp reader can be good for starters.

How about Appendex C of CLTL2?

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
From: Emre Sevinc
Subject: Re: nested quasiquote?
Date: 
Message-ID: <87zmpk317m.fsf@ileriseviye.org>
Barry Margolin <······@alum.mit.edu> writes:

> In article <··············@ileriseviye.org>,
>  Emre Sevinc <·····@bilgi.edu.tr> wrote:
>
>> Maybe a step by step explanation of how ```,,,g
>> is read by Lisp reader can be good for starters.
>
> How about Appendex C of CLTL2?

Pure wisdom :)

I think two of the most important parts from CLtL2 Appendix C
and HyperSpec are:

- (Note that in the expression `(foo ,(process `(bar ,x))) the backquotes 
are not doubly nested. The inner backquoted expression occurs within the 
textual scope of a comma belonging to the outer backquote. The correct 
way to determine the backquote nesting level of any subexpression is to 
start a count at zero and proceed up the S-expression tree, adding one for
each backquote and subtracting one for each comma. This is similar to the 
rule for determining nesting level with respect to parentheses by scanning 
a character string linearly, adding or subtracting one as parentheses are passed.)

- If the backquote syntax is nested, the innermost backquoted form should be 
expanded first. This means that if several commas occur in a row, the leftmost 
one belongs to the innermost backquote.



-- 
Emre Sevinc

eMBA Software Developer         Actively engaged in:
http:www.bilgi.edu.tr           http://ileriseviye.org
http://www.bilgi.edu.tr         http://fazlamesai.net
Cognitive Science Student       http://cazci.com
http://www.cogsci.boun.edu.tr
From: Timofei Shatrov
Subject: Re: nested quasiquote?
Date: 
Message-ID: <43476f71.2749386@news.readfreenews.net>
On Fri, 7 Oct 2005 13:53:46 -0500, Adrian Kubala
<···········@sixfingeredman.net> tried to confuse everyone with this
message:

>So I was playing with the following definition of once-only, and am
>mystified by the use of ``(,,g ,,n):
>
>(defmacro once-only ((&rest names) &body body)
>  (let ((gensyms (loop for n in names collect (gensym))))
>    `(let (,@(loop for g in gensyms for n in names collect `(,g (gensym ,(symbol-name n)))))
>      `(let (,,@(loop for g in gensyms for n in names collect ``(,,g ,,n)))
>        ,(let (,@(loop for n in names for g in gensyms collect `(,n ,g)))
>           ,@body)))))
>
>Would somebody be kind enough to clarify why this works differently
>than, for example, `(,g ,n) or ```(,,,g ,,,n)? Does anybody know a good
>tutorial that discusses the subtleties of nested-quasiquote and/or
>macro-writing macros?

Graham's "On Lisp" has a method how to construct such macro-creating
macros.

-- 
|a\o/r|,-------------.,---------- Timofei Shatrov aka Grue ------------.
| m"a ||FC AMKAR PERM|| mail: grue at mail.ru  http://grue3.tripod.com |
|  k  ||  PWNZ J00   || Kingdom of Loathing: Grue3 lvl 18 Seal Clubber |
`-----'`-------------'`-------------------------------------------[4*72]