Nested bacquotes are so hard (at least for me :) )!

This is a discussion on Nested bacquotes are so hard (at least for me :) )! within the lisp forums in Programming Languages category; I was doing some experimenting with MACROLET, just to figure out how it works a little better, but I can't get the following code to work. It only prints "Hello World!" once instead of 10 times. There's no specific goal for this code. I was just experimenting for learning purposes only. (macrolet ((with-ps-gensyms ((&rest names) &body body) `(let ,(loop for n in names collect `(,n (gensym))) ,@body)) (ntimes (n &rest body) `(with-ps-gensyms (g h) `(let ((,h ,,n)) (do ((,g 0 (+ ,g 1))) ((>= ,g ,h)) ,,@body))))) (ntimes 10 (format t "Hello World!~%")))...

Go Back   Application Development Forum > Programming Languages > lisp

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-21-2008, 09:09 AM
rock69
Guest
 
Default Nested bacquotes are so hard (at least for me :) )!

I was doing some experimenting with MACROLET, just to figure out how
it works a little better, but I can't get the following code to work.
It only prints "Hello World!" once instead of 10 times. There's no
specific goal for this code. I was just experimenting for learning
purposes only.

(macrolet ((with-ps-gensyms ((&rest names) &body body)
`(let ,(loop for n in names collect `(,n (gensym)))
,@body))
(ntimes (n &rest body)
`(with-ps-gensyms (g h)
`(let ((,h ,,n))
(do ((,g 0 (+ ,g 1)))
((>= ,g ,h))
,,@body)))))
(ntimes 10 (format t "Hello World!~%")))
Reply With Quote
  #2  
Old 08-21-2008, 09:58 AM
rock69
Guest
 
Default Re: Nested bacquotes are so hard (at least for me :) )!

Tinkering a little, I've just discovered that it expands correctly by
replacing ,,@body with ,',@body which seems logical of course yet
it still requires one more evaluation step. In other words, it works
only if I replace the invocation (ntimes 10 (format t "Hello World!~
%")) with (eval (ntimes 10 (format t "Hello World!~%"))) which is of
course a ridiculous thing to have to do, so I'm still pretty much
stuck, only a little closer maybe to the solution.

rock69 wrote:
> I was doing some experimenting with MACROLET, just to figure out how
> it works a little better, but I can't get the following code to work.
> It only prints "Hello World!" once instead of 10 times. There's no
> specific goal for this code. I was just experimenting for learning
> purposes only.
>
> (macrolet ((with-ps-gensyms ((&rest names) &body body)
> `(let ,(loop for n in names collect `(,n (gensym)))
> ,@body))
> (ntimes (n &rest body)
> `(with-ps-gensyms (g h)
> `(let ((,h ,,n))
> (do ((,g 0 (+ ,g 1)))
> ((>= ,g ,h))
> ,,@body)))))
> (ntimes 10 (format t "Hello World!~%")))

Reply With Quote
  #3  
Old 08-21-2008, 10:12 AM
rock69
Guest
 
Default Re: Nested bacquotes are so hard (at least for me :) )!

On Aug 21, 3:58*pm, rock69 <rocco.ro...@gmail.com> wrote:
> Tinkering a little, I've just discovered that it expands correctly by
> replacing ,,@body with ,',@body which seems logical of course yet
> it still requires one more evaluation step. In other words, it works
> only if I replace the invocation (ntimes 10 (format t "Hello World!~
> %")) with (eval (ntimes 10 (format t "Hello World!~%"))) which is of
> course a ridiculous thing to have to do, so I'm still pretty much
> stuck, only a little closer maybe to the solution.
>
> rock69 wrote:
> > I was doing some experimenting with MACROLET, just to figure out how
> > it works a little better, but I can't get the following code to work.
> > It only prints "Hello World!" once instead of 10 times. There's no
> > specific goal for this code. I was just experimenting for learning
> > purposes only.

>
> > (macrolet ((with-ps-gensyms ((&rest names) &body body)
> > * * * * * * * * * * * *`(let ,(loop for n in names collect `(,n (gensym)))
> > * * * * * * * * * * * * * ,@body))
> > * * * (ntimes (n &rest body)
> > * * * * * * * `(with-ps-gensyms (g h)
> > * * * * * * * * * * * * * * * * `(let ((,h ,,n))
> > * * * * * * * * * * * * * * * * * *(do ((,g 0 (+ ,g 1)))
> > * * * * * * * * * * * * * * * * * ** *((>= ,g ,h))
> > * * * * * * * * * * * * * * * * * ** *,,@body)))))
> > * * *(ntimes 10 (format t "Hello World!~%")))

>
>


OK, I got it all wrong. It turns out I didn't need nested backquotes
in the first place. This works fine:

(defmacro ntimes (n &rest body)
`(with-ps-gensyms (g h)
(let ((,h ,n))
(do ((,g 0 (+ ,g 1)))
((>= ,g ,h))
,@body))))

Looks like I've got some more studying to do

Does anyone know where I can find some really extensive info on
backquote syntax? I've seen the HyperSpec, On Lisp, and ANSI Common
Lisp, but it still feels like something's missing. Mr. Pitman help,
please.
Reply With Quote
  #4  
Old 08-21-2008, 10:25 AM
rock69
Guest
 
Default Re: Nested bacquotes are so hard (at least for me :) )!

On Aug 21, 4:12*pm, rock69 <rocco.ro...@gmail.com> wrote:
> On Aug 21, 3:58*pm, rock69 <rocco.ro...@gmail.com> wrote:
>
>
>
> > Tinkering a little, I've just discovered that it expands correctly by
> > replacing ,,@body with ,',@body which seems logical of course yet
> > it still requires one more evaluation step. In other words, it works
> > only if I replace the invocation (ntimes 10 (format t "Hello World!~
> > %")) with (eval (ntimes 10 (format t "Hello World!~%"))) which is of
> > course a ridiculous thing to have to do, so I'm still pretty much
> > stuck, only a little closer maybe to the solution.

>
> > rock69 wrote:
> > > I was doing some experimenting with MACROLET, just to figure out how
> > > it works a little better, but I can't get the following code to work.
> > > It only prints "Hello World!" once instead of 10 times. There's no
> > > specific goal for this code. I was just experimenting for learning
> > > purposes only.

>
> > > (macrolet ((with-ps-gensyms ((&rest names) &body body)
> > > * * * * * * * * * * * *`(let ,(loop for n in names collect `(,n (gensym)))
> > > * * * * * * * * * * * * * ,@body))
> > > * * * (ntimes (n &rest body)
> > > * * * * * * * `(with-ps-gensyms (g h)
> > > * * * * * * * * * * * * * * * * `(let((,h ,,n))
> > > * * * * * * * * * * * * * * * * * *(do ((,g 0 (+ ,g 1)))
> > > * * * * * * * * * * * * * * * * * * * *((>= ,g ,h))
> > > * * * * * * * * * * * * * * * * * * * *,,@body)))))
> > > * * *(ntimes 10 (format t "Hello World!~%")))

>
> OK, I got it all wrong. It turns out I didn't need nested backquotes
> in the first place. This works fine:
>
> (defmacro ntimes (n &rest body)
> * * * * * * * * * *`(with-ps-gensyms (g h)
> * * * * * * * * * * * * * * * * * * *(let ((,h ,n))
> * * * * * * * * * * * * * * * * * * * * (do ((,g 0 (+ ,g 1)))
> * * * * * * * * * * * * * * * * * * * * * * ((>= ,g ,h))
> * * * * * * * * * * * * * * * * * * * * * * ,@body))))
>
> Looks like I've got some more studying to do
>
> Does anyone know where I can find some really extensive info on
> backquote syntax? I've seen the HyperSpec, On Lisp, and ANSI Common
> Lisp, but it still feels like something's missing. Mr. Pitman help,
> please.


Oops, the correct code is the following, sorry ...

(macrolet ((with-ps-gensyms ((&rest names) &body body)
`(let ,(loop for n in names collect `(,n (gensym)))
,@body))
(ntimes (n &rest body)
`(with-ps-gensyms (g h)
(let ((,h ,n))
(do ((,g 0 (+ ,g 1)))
((>= ,g ,h))
,@body)))))
(ntimes 10 (format t "Hello World!~%")))
Reply With Quote
  #5  
Old 08-21-2008, 10:27 AM
Kenny
Guest
 
Default Re: Nested bacquotes are so hard (at least for me :) )!

rock69 wrote:
> On Aug 21, 3:58 pm, rock69 <rocco.ro...@gmail.com> wrote:
>
>>Tinkering a little, I've just discovered that it expands correctly by
>>replacing ,,@body with ,',@body which seems logical of course yet
>>it still requires one more evaluation step. In other words, it works
>>only if I replace the invocation (ntimes 10 (format t "Hello World!~
>>%")) with (eval (ntimes 10 (format t "Hello World!~%"))) which is of
>>course a ridiculous thing to have to do, so I'm still pretty much
>>stuck, only a little closer maybe to the solution.
>>
>>rock69 wrote:
>>
>>>I was doing some experimenting with MACROLET, just to figure out how
>>>it works a little better, but I can't get the following code to work.
>>>It only prints "Hello World!" once instead of 10 times. There's no
>>>specific goal for this code. I was just experimenting for learning
>>>purposes only.

>>
>>>(macrolet ((with-ps-gensyms ((&rest names) &body body)
>>> `(let ,(loop for n in names collect `(,n (gensym)))
>>> ,@body))
>>> (ntimes (n &rest body)
>>> `(with-ps-gensyms (g h)
>>> `(let ((,h ,,n))
>>> (do ((,g 0 (+ ,g 1)))
>>> ((>= ,g ,h))
>>> ,,@body)))))
>>> (ntimes 10 (format t "Hello World!~%")))

>>
>>

>
> OK, I got it all wrong. It turns out I didn't need nested backquotes
> in the first place. This works fine:
>
> (defmacro ntimes (n &rest body)
> `(with-ps-gensyms (g h)
> (let ((,h ,n))
> (do ((,g 0 (+ ,g 1)))
> ((>= ,g ,h))
> ,@body))))
>
> Looks like I've got some more studying to do
>
> Does anyone know where I can find some really extensive info on
> backquote syntax? I've seen the HyperSpec, On Lisp, and ANSI Common
> Lisp, but it still feels like something's missing. Mr. Pitman help,
> please.


Do what I do, just keep adding and removing quotes and ticks and commas
and moving them around at the same time until it works. This is where
the monkeys really earn their bananas. Sometimes I break stuff out into
a second macro just to keep my sanity. When it gets depressing, just
remember, you could be trying to use Scheme to do macros. When you are
ready to kill yourself, use F#.

hth, kenny
Reply With Quote
  #6  
Old 08-21-2008, 11:02 AM
rock69
Guest
 
Default Re: Nested bacquotes are so hard (at least for me :) )!

On Aug 21, 4:27*pm, Kenny <kentil...@gmail.com> wrote:
> rock69 wrote:
> > On Aug 21, 3:58 pm, rock69 <rocco.ro...@gmail.com> wrote:

>
> >>Tinkering a little, I've just discovered that it expands correctly by
> >>replacing ,,@body with ,',@body which seems logical of course yet
> >>it still requires one more evaluation step. In other words, it works
> >>only if I replace the invocation (ntimes 10 (format t "Hello World!~
> >>%")) with (eval (ntimes 10 (format t "Hello World!~%"))) which is of
> >>course a ridiculous thing to have to do, so I'm still pretty much
> >>stuck, only a little closer maybe to the solution.

>
> >>rock69 wrote:

>
> >>>I was doing some experimenting with MACROLET, just to figure out how
> >>>it works a little better, but I can't get the following code to work.
> >>>It only prints "Hello World!" once instead of 10 times. There's no
> >>>specific goal for this code. I was just experimenting for learning
> >>>purposes only.

>
> >>>(macrolet ((with-ps-gensyms ((&rest names) &body body)
> >>> * * * * * * * * * * * `(let ,(loop for n in names collect `(,n (gensym)))
> >>> * * * * * * * * * * * * *,@body))
> >>> * * *(ntimes (n &rest body)
> >>> * * * * * * *`(with-ps-gensyms (g h)
> >>> * * * * * * * * * * * * * * * *`(let ((,h ,,n))
> >>> * * * * * * * * * * * * * * * * * (do ((,g 0 (+ ,g 1)))
> >>> * * * * * * * * * * * * * * * * * * * ((>= ,g ,h))
> >>> * * * * * * * * * * * * * * * * * * * ,,@body)))))
> >>> * * (ntimes 10 (format t "Hello World!~%")))

>
> > OK, I got it all wrong. It turns out I didn't need nested backquotes
> > in the first place. This works fine:

>
> > (defmacro ntimes (n &rest body)
> > * * * * * * * * * *`(with-ps-gensyms (g h)
> > * * * * * * * * * * * * * * * * * **(let ((,h ,n))
> > * * * * * * * * * * * * * * * * * ** * (do ((,g 0 (+ ,g 1)))
> > * * * * * * * * * * * * * * * * * ** * * * ((>= ,g ,h))
> > * * * * * * * * * * * * * * * * * ** * * * ,@body))))

>
> > Looks like I've got some more studying to do

>
> > Does anyone know where I can find some really extensive info on
> > backquote syntax? I've seen the HyperSpec, On Lisp, and ANSI Common
> > Lisp, but it still feels like something's missing. Mr. Pitman help,
> > please.

>
> Do what I do, just keep adding and removing quotes and ticks and commas
> and moving them around at the same time until it works. This is where
> the monkeys really earn their bananas. Sometimes I break stuff out into
> a second macro just to keep my sanity. When it gets depressing, just
> remember, you could be trying to use Scheme to do macros. When you are
> ready to kill yourself, use F#.
>
> hth, kenny


Yeah, I guess it really is just a matter of trial and error. Like
Graham says, it's like trying to figure out a complex integral by just
looking at it.

F# no way! LOL

Reply With Quote
  #7  
Old 08-21-2008, 11:25 AM
rock69
Guest
 
Default Re: Nested bacquotes are so hard (at least for me :) )!

On Aug 21, 5:02*pm, rock69 <rocco.ro...@gmail.com> wrote:
> On Aug 21, 4:27*pm, Kenny <kentil...@gmail.com> wrote:
>
>
>
> > rock69 wrote:
> > > On Aug 21, 3:58 pm, rock69 <rocco.ro...@gmail.com> wrote:

>
> > >>Tinkering a little, I've just discovered that it expands correctly by
> > >>replacing ,,@body with ,',@body which seems logical of course yet
> > >>it still requires one more evaluation step. In other words, it works
> > >>only if I replace the invocation (ntimes 10 (format t "Hello World!~
> > >>%")) with (eval (ntimes 10 (format t "Hello World!~%"))) which is of
> > >>course a ridiculous thing to have to do, so I'm still pretty much
> > >>stuck, only a little closer maybe to the solution.

>
> > >>rock69 wrote:

>
> > >>>I was doing some experimenting with MACROLET, just to figure out how
> > >>>it works a little better, but I can't get the following code to work..
> > >>>It only prints "Hello World!" once instead of 10 times. There's no
> > >>>specific goal for this code. I was just experimenting for learning
> > >>>purposes only.

>
> > >>>(macrolet ((with-ps-gensyms ((&rest names) &body body)
> > >>> * * * * * * * * * * * `(let ,(loop for n in names collect `(,n (gensym)))
> > >>> * * * * * * * * * * * * *,@body))
> > >>> * * *(ntimes (n &rest body)
> > >>> * * * * * * *`(with-ps-gensyms (g h)
> > >>> * * * * * * * * * * * * * * * *`(let ((,h ,,n))
> > >>> * * * * * * * * * * * * * * * * *(do ((,g 0 (+ ,g 1)))
> > >>> * * * * * * * * * * * * * * * * ** * ((>= ,g ,h))
> > >>> * * * * * * * * * * * * * * * * ** * ,,@body)))))
> > >>> * * (ntimes 10 (format t "Hello World!~%")))

>
> > > OK, I got it all wrong. It turns out I didn't need nested backquotes
> > > in the first place. This works fine:

>
> > > (defmacro ntimes (n &rest body)
> > > * * * * * * * * * *`(with-ps-gensyms (g h)
> > > * * * * * * * * * * * * * * * * * * *(let ((,h ,n))
> > > * * * * * * * * * * * * * * * * * * * * (do ((,g 0 (+ ,g 1)))
> > > * * * * * * * * * * * * * * * * * * * * * * ((>= ,g ,h))
> > > * * * * * * * * * * * * * * * * * * * * * * ,@body))))

>
> > > Looks like I've got some more studying to do

>
> > > Does anyone know where I can find some really extensive info on
> > > backquote syntax? I've seen the HyperSpec, On Lisp, and ANSI Common
> > > Lisp, but it still feels like something's missing. Mr. Pitman help,
> > > please.

>
> > Do what I do, just keep adding and removing quotes and ticks and commas
> > and moving them around at the same time until it works. This is where
> > the monkeys really earn their bananas. Sometimes I break stuff out into
> > a second macro just to keep my sanity. When it gets depressing, just
> > remember, you could be trying to use Scheme to do macros. When you are
> > ready to kill yourself, use F#.

>
> > hth, kenny

>
> Yeah, I guess it really is just a matter of trial and error. Like
> Graham says, it's like trying to figure out a complex integral by just
> looking at it.
>
> F# no way! LOL


Now this REALLY puzzles me. Going back to the previous version and
feeding it to SBCL this is what I get:

> (macrolet ((with-ps-gensyms ((&rest names) &body body)

`(let ,(loop for n in names collect `(,n (gensym)))
,@body))
(ntimes (n &rest body)
`(with-ps-gensyms (g h)
`(let ((,h ,',n))
(do ((,g 0 (+ ,g 1)))
((>= ,g ,h))
,',@body)))))
(ntimes 10 (format t "Hello World!~%")))

OUTPUT:

(LET ((#:G605 10))
(DO ((#:G604 0 (+ #:G604 1)))
((>= #:G604 #:G605))
(FORMAT T "Hello World!~%")))

Like I said, it doesn't get evaluated, but the expansion appears to be
correct. Why doesn't it continue and evaluate it instead of stopping
at this point???
Reply With Quote
  #8  
Old 08-21-2008, 12:56 PM
Thomas A. Russ
Guest
 
Default Re: Nested bacquotes are so hard (at least for me :) )!

rock69 <rocco.rossi@gmail.com> writes:

> Now this REALLY puzzles me. Going back to the previous version and
> feeding it to SBCL this is what I get:
>
> > (macrolet ((with-ps-gensyms ((&rest names) &body body)

> `(let ,(loop for n in names collect `(,n (gensym)))
> ,@body))
> (ntimes (n &rest body)
> `(with-ps-gensyms (g h)
> `(let ((,h ,',n))
> (do ((,g 0 (+ ,g 1)))


^ ^

Um, you have two levels of (back)quoting going on. That is what is
stopping the evaluation. You want to drop the backquote in front of the
WITH-PS-GENSYMS invocation. You want that macro to be expanded as an
aid to the generation of the macro-expansion of NTIMES. You don't want
it to be part of that macroexpansion.

What I'm not sure of (and am too lazy to check the Hyperspect to find
out) is whether MACROLET works like FLET or LABELS as far as scope is
concerned. You might need to use nested MACROLET forms if you can't use
the first macro definition while doing the expansion of the second.

> ((>= ,g ,h))
> ,',@body)))))
> (ntimes 10 (format t "Hello World!~%")))


> OUTPUT:
>
> (LET ((#:G605 10))
> (DO ((#:G604 0 (+ #:G604 1)))
> ((>= #:G604 #:G605))
> (FORMAT T "Hello World!~%")))
>
> Like I said, it doesn't get evaluated, but the expansion appears to be
> correct. Why doesn't it continue and evaluate it instead of stopping
> at this point???


Well, when debugging things like this, it is often useful to make these
be top-level macro definitions and then use MACROEXPAND and
MACROEXPAND-1 on them.


--
Thomas A. Russ, USC/Information Sciences Institute
Reply With Quote
  #9  
Old 08-21-2008, 01:07 PM
Geoffrey Summerhayes
Guest
 
Default Re: Nested bacquotes are so hard (at least for me :) )!

On Aug 21, 11:25*am, rock69 <rocco.ro...@gmail.com> wrote:
> On Aug 21, 5:02*pm, rock69 <rocco.ro...@gmail.com> wrote:
>
>
>
>
>
> > On Aug 21, 4:27*pm, Kenny <kentil...@gmail.com> wrote:

>
> > > rock69 wrote:
> > > > On Aug 21, 3:58 pm, rock69 <rocco.ro...@gmail.com> wrote:

>
> > > >>Tinkering a little, I've just discovered that it expands correctly by
> > > >>replacing ,,@body with ,',@body which seems logical of course yet
> > > >>it still requires one more evaluation step. In other words, it works
> > > >>only if I replace the invocation (ntimes 10 (format t "Hello World!~
> > > >>%")) with (eval (ntimes 10 (format t "Hello World!~%"))) which is of
> > > >>course a ridiculous thing to have to do, so I'm still pretty much
> > > >>stuck, only a little closer maybe to the solution.

>
> > > >>rock69 wrote:

>
> > > >>>I was doing some experimenting with MACROLET, just to figure out how
> > > >>>it works a little better, but I can't get the following code to work.
> > > >>>It only prints "Hello World!" once instead of 10 times. There's no
> > > >>>specific goal for this code. I was just experimenting for learning
> > > >>>purposes only.

>
> > > >>>(macrolet ((with-ps-gensyms ((&rest names) &body body)
> > > >>> * * * * * * * * * * * `(let ,(loop for n innames collect `(,n (gensym)))
> > > >>> * * * * * * * * * * * * *,@body))
> > > >>> * * *(ntimes (n &rest body)
> > > >>> * * * * * * *`(with-ps-gensyms (g h)
> > > >>> * * * * * * * * * * * * * * * *`(let ((,h ,,n))
> > > >>> * * * * * * * * * * * * * * * * * (do ((,g 0 (+ ,g 1)))
> > > >>> * * * * * * * * * * * * * * * * * * * ((>= ,g ,h))
> > > >>> * * * * * * * * * * * * * * * * * * * ,,@body)))))
> > > >>> * * (ntimes 10 (format t "Hello World!~%")))

>
> > > > OK, I got it all wrong. It turns out I didn't need nested backquotes
> > > > in the first place. This works fine:

>
> > > > (defmacro ntimes (n &rest body)
> > > > * * * * * * * * * *`(with-ps-gensyms (g h)
> > > > * * * * * * * * * * * * * * * * ** *(let ((,h ,n))
> > > > * * * * * * * * * * * * * * * * ** * * (do ((,g 0 (+ ,g 1)))
> > > > * * * * * * * * * * * * * * * * ** * * * * ((>= ,g ,h))
> > > > * * * * * * * * * * * * * * * * ** * * * * ,@body))))

>
> > > > Looks like I've got some more studying to do

>
> > > > Does anyone know where I can find some really extensive info on
> > > > backquote syntax? I've seen the HyperSpec, On Lisp, and ANSI Common
> > > > Lisp, but it still feels like something's missing. Mr. Pitman help,
> > > > please.

>
> > > Do what I do, just keep adding and removing quotes and ticks and commas
> > > and moving them around at the same time until it works. This is where
> > > the monkeys really earn their bananas. Sometimes I break stuff out into
> > > a second macro just to keep my sanity. When it gets depressing, just
> > > remember, you could be trying to use Scheme to do macros. When you are
> > > ready to kill yourself, use F#.

>
> > > hth, kenny

>
> > Yeah, I guess it really is just a matter of trial and error. Like
> > Graham says, it's like trying to figure out a complex integral by just
> > looking at it.

>
> > F# no way! LOL

>
> Now this REALLY puzzles me. Going back to the previous version and
> feeding it to SBCL this is what I get:
>
> > (macrolet ((with-ps-gensyms ((&rest names) &body body)

>
> * * `(let ,(loop for n in names collect `(,n (gensym)))
> * * * *,@body))
> * *(ntimes (n &rest body)
> * *`(with-ps-gensyms (g h)
> * * *`(let ((,h ,',n))
> * * * *(do ((,g 0 (+ ,g 1)))
> * *((>= ,g ,h))
> * *,',@body)))))
> * (ntimes 10 (format t "Hello World!~%")))
>
> OUTPUT:
>
> (LET ((#:G605 10))
> * (DO ((#:G604 0 (+ #:G604 1)))
> * * * ((>= #:G604 #:G605))
> * * (FORMAT T "Hello World!~%")))
>
> Like I said, it doesn't get evaluated, but the expansion appears to be
> correct. Why doesn't it continue and evaluate it instead of stopping
> at this point???- Hide quoted text -


Actually what came out was evaluated, it's just that what came
out was quoted.

(macrolet ((with-ps-gensyms ((&rest names) &body body)
`(let ,(loop for n in names collect `(,n (gensym)))
,@body))
(ntimes (n &rest body)
`(with-ps-gensyms (g h)
(let ((h ,n))
(do ((g 0 (+ g 1)))
((>= g h))
,@body)))))
(ntimes 10 (format t "Hello World!~%")))

----
Geoff
Reply With Quote
  #10  
Old 08-21-2008, 01:11 PM
rock69
Guest
 
Default Re: Nested bacquotes are so hard (at least for me :) )!

On Aug 21, 7:07*pm, Geoffrey Summerhayes <sumr...@gmail.com> wrote:
> On Aug 21, 11:25*am, rock69 <rocco.ro...@gmail.com> wrote:
>
>
>
> > On Aug 21, 5:02*pm, rock69 <rocco.ro...@gmail.com> wrote:

>
> > > On Aug 21, 4:27*pm, Kenny <kentil...@gmail.com> wrote:

>
> > > > rock69 wrote:
> > > > > On Aug 21, 3:58 pm, rock69 <rocco.ro...@gmail.com> wrote:

>
> > > > >>Tinkering a little, I've just discovered that it expands correctly by
> > > > >>replacing ,,@body with ,',@body which seems logical of course yet
> > > > >>it still requires one more evaluation step. In other words, it works
> > > > >>only if I replace the invocation (ntimes 10 (format t "Hello World!~
> > > > >>%")) with (eval (ntimes 10 (format t "Hello World!~%"))) which isof
> > > > >>course a ridiculous thing to have to do, so I'm still pretty much
> > > > >>stuck, only a little closer maybe to the solution.

>
> > > > >>rock69 wrote:

>
> > > > >>>I was doing some experimenting with MACROLET, just to figure outhow
> > > > >>>it works a little better, but I can't get the following code to work.
> > > > >>>It only prints "Hello World!" once instead of 10 times. There's no
> > > > >>>specific goal for this code. I was just experimenting for learning
> > > > >>>purposes only.

>
> > > > >>>(macrolet ((with-ps-gensyms ((&rest names) &body body)
> > > > >>> * * * * * * * * * * * `(let ,(loop for n in names collect `(,n (gensym)))
> > > > >>> * * * * * * * * * * * * *,@body))
> > > > >>> * * *(ntimes (n &rest body)
> > > > >>> * * * * * * *`(with-ps-gensyms (g h)
> > > > >>> * * * * * * * * * * * * * * * *`(let ((,h ,,n))
> > > > >>> * * * * * * * * * * * * * * * ** (do ((,g 0 (+ ,g 1)))
> > > > >>> * * * * * * * * * * * * * * * ** * * ((>= ,g ,h))
> > > > >>> * * * * * * * * * * * * * * * ** * * ,,@body)))))
> > > > >>> * * (ntimes 10 (format t "Hello World!~%")))

>
> > > > > OK, I got it all wrong. It turns out I didn't need nested backquotes
> > > > > in the first place. This works fine:

>
> > > > > (defmacro ntimes (n &rest body)
> > > > > * * * * * * * * * *`(with-ps-gensyms (g h)
> > > > > * * * * * * * * * * * * * * * * * * *(let ((,h ,n))
> > > > > * * * * * * * * * * * * * * * * * * * * (do ((,g 0 (+ ,g 1)))
> > > > > * * * * * * * * * * * * * * * * * * * * * * ((>= ,g ,h))
> > > > > * * * * * * * * * * * * * * * * * * * * * * ,@body))))

>
> > > > > Looks like I've got some more studying to do

>
> > > > > Does anyone know where I can find some really extensive info on
> > > > > backquote syntax? I've seen the HyperSpec, On Lisp, and ANSI Common
> > > > > Lisp, but it still feels like something's missing. Mr. Pitman help,
> > > > > please.

>
> > > > Do what I do, just keep adding and removing quotes and ticks and commas
> > > > and moving them around at the same time until it works. This is where
> > > > the monkeys really earn their bananas. Sometimes I break stuff out into
> > > > a second macro just to keep my sanity. When it gets depressing, just
> > > > remember, you could be trying to use Scheme to do macros. When you are
> > > > ready to kill yourself, use F#.

>
> > > > hth, kenny

>
> > > Yeah, I guess it really is just a matter of trial and error. Like
> > > Graham says, it's like trying to figure out a complex integral by just
> > > looking at it.

>
> > > F# no way! LOL

>
> > Now this REALLY puzzles me. Going back to the previous version and
> > feeding it to SBCL this is what I get:

>
> > > (macrolet ((with-ps-gensyms ((&rest names) &body body)

>
> > * * `(let ,(loop for n in names collect `(,n (gensym)))
> > * * * *,@body))
> > * *(ntimes (n &rest body)
> > * *`(with-ps-gensyms (g h)
> > * * *`(let ((,h ,',n))
> > * * * *(do ((,g 0 (+ ,g 1)))
> > * *((>= ,g ,h))
> > * *,',@body)))))
> > * (ntimes 10 (format t "Hello World!~%")))

>
> > OUTPUT:

>
> > (LET ((#:G605 10))
> > * (DO ((#:G604 0 (+ #:G604 1)))
> > * * * ((>= #:G604 #:G605))
> > * * (FORMAT T "Hello World!~%")))

>
> > Like I said, it doesn't get evaluated, but the expansion appears to be
> > correct. Why doesn't it continue and evaluate it instead of stopping
> > at this point???- Hide quoted text -

>
> Actually what came out was evaluated, it's just that what came
> out was quoted.
>
> (macrolet ((with-ps-gensyms ((&rest names) &body body)
> * * * * * * *`(let ,(loop for n in names collect `(,n (gensym)))
> * * * * * * * * ,@body))
> * * * * * *(ntimes (n &rest body)
> * * * * * * *`(with-ps-gensyms (g h)
> * * * * * * * * * * * * * * * (let ((h ,n))
> * * * * * * * * * * * * * * * * *(do ((g 0 (+ g 1)))
> * * * * * * * * * * * * * * * * * * *((>= g h))
> * * * * * * * * * * * * * * * * * * ,@body)))))
> * (ntimes 10 (format t "Hello World!~%")))
>
> ----
> Geoff


OK Geoff, thanks. I see what you mean.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 04:37 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.