* Need help with a macro
@ 2009-11-15 16:33 Josef Wolf
2009-11-15 18:18 ` Andy Wingo
2009-11-15 20:26 ` Neil Jerram
0 siblings, 2 replies; 5+ messages in thread
From: Josef Wolf @ 2009-11-15 16:33 UTC (permalink / raw)
To: guile-user
Hello,
I am trying to work through the little schemer book. In order to make it
easier to go through the examples, I've come up with the following macro:
;;; Helper to visualize
(define-macro (disp exp)
(display exp) (newline)
`(display ,exp)
; (newline) (newline)
)
This works great, except that when I uncomment the (newline) expressions,
the result of the evaluation is omitted:
guile> (define-macro (disp exp)
... (display exp) (newline)
... `(display ,exp)
... ; (newline) (newline)
... )
guile> (disp (+ 3 4)) (newline)
(+ 3 4)
7
guile> (define-macro (disp exp)
... (display exp) (newline)
... `(display ,exp)
... (newline) (newline)
... )
guile> (disp (+ 3 4)) (newline)
(+ 3 4)
guile>
Any hints?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Need help with a macro
2009-11-15 16:33 Need help with a macro Josef Wolf
@ 2009-11-15 18:18 ` Andy Wingo
2009-11-16 17:40 ` Josef Wolf
2009-11-15 20:26 ` Neil Jerram
1 sibling, 1 reply; 5+ messages in thread
From: Andy Wingo @ 2009-11-15 18:18 UTC (permalink / raw)
To: Josef Wolf; +Cc: guile-user
Hello,
On Sun 15 Nov 2009 17:33, Josef Wolf <jw@raven.inka.de> writes:
> Hello,
>
> I am trying to work through the little schemer book. In order to make it
> easier to go through the examples, I've come up with the following macro:
>
> ;;; Helper to visualize
>
> (define-macro (disp exp)
> (display exp) (newline)
> `(display ,exp)
> ; (newline) (newline)
> )
Does the little schemer book actually use defmacro?
Anyway the issue is that defmacro needs to return Scheme code as an
s-expression. The last expression in a function is its return value.
That would be the `(display ,exp) bit there. But if you uncomment the
newlines, well, you return whatever newline returns, which is actually
unspecified.
Good luck,
Andy
--
http://wingolog.org/
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Need help with a macro
2009-11-15 16:33 Need help with a macro Josef Wolf
2009-11-15 18:18 ` Andy Wingo
@ 2009-11-15 20:26 ` Neil Jerram
2009-11-16 17:20 ` Josef Wolf
1 sibling, 1 reply; 5+ messages in thread
From: Neil Jerram @ 2009-11-15 20:26 UTC (permalink / raw)
To: Josef Wolf; +Cc: guile-user
Josef Wolf <jw@raven.inka.de> writes:
> Hello,
>
> I am trying to work through the little schemer book. In order to make it
> easier to go through the examples, I've come up with the following macro:
>
> ;;; Helper to visualize
>
> (define-macro (disp exp)
> (display exp) (newline)
> `(display ,exp)
> ; (newline) (newline)
> )
>
> This works great, except that when I uncomment the (newline) expressions,
> the result of the evaluation is omitted:
>
> guile> (define-macro (disp exp)
> ... (display exp) (newline)
> ... `(display ,exp)
> ... ; (newline) (newline)
> ... )
> guile> (disp (+ 3 4)) (newline)
> (+ 3 4)
> 7
> guile> (define-macro (disp exp)
> ... (display exp) (newline)
> ... `(display ,exp)
> ... (newline) (newline)
> ... )
> guile> (disp (+ 3 4)) (newline)
> (+ 3 4)
>
>
>
> guile>
>
> Any hints?
The body of a define-macro definition is supposed to return the code
that should be substituted in place of the original macro call. With
the newlines there, the body returns *unspecified*, because that is what
(newline) returns.
Do you want those newlines to happen at macro-expansion time or eval
time? If the latter (which I would guess), the code that you want is
(define-macro (disp exp)
(display exp) (newline)
`(begin
(display ,exp)
(newline) (newline)))
Regards,
Neil
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Need help with a macro
2009-11-15 20:26 ` Neil Jerram
@ 2009-11-16 17:20 ` Josef Wolf
0 siblings, 0 replies; 5+ messages in thread
From: Josef Wolf @ 2009-11-16 17:20 UTC (permalink / raw)
To: guile-user
On Sun, Nov 15, 2009 at 08:26:58PM +0000, Neil Jerram wrote:
> Josef Wolf <jw@raven.inka.de> writes:
> > I am trying to work through the little schemer book. In order to make it
> > easier to go through the examples, I've come up with the following macro:
> > [ ... ]
>
> The body of a define-macro definition is supposed to return the code
> that should be substituted in place of the original macro call. With
> the newlines there, the body returns *unspecified*, because that is what
> (newline) returns.
OK, I think I see the problem now...
> Do you want those newlines to happen at macro-expansion time or eval
> time? If the latter (which I would guess), the code that you want is
>
> (define-macro (disp exp)
> (display exp) (newline)
> `(begin
> (display ,exp)
> (newline) (newline)))
Works great. Thanks!
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Need help with a macro
2009-11-15 18:18 ` Andy Wingo
@ 2009-11-16 17:40 ` Josef Wolf
0 siblings, 0 replies; 5+ messages in thread
From: Josef Wolf @ 2009-11-16 17:40 UTC (permalink / raw)
To: guile-user
On Sun, Nov 15, 2009 at 07:18:33PM +0100, Andy Wingo wrote:
> On Sun 15 Nov 2009 17:33, Josef Wolf <jw@raven.inka.de> writes:
> > I am trying to work through the little schemer book. In order to make it
> > easier to go through the examples, I've come up with the following macro:
> > [ ... ]
>
> Does the little schemer book actually use defmacro?
It does not. But I use it only to avoid typing everything twice. I think it
is allowed for such usage ;-)
> Anyway the issue is that defmacro needs to return Scheme code as an
> s-expression. The last expression in a function is its return value.
> That would be the `(display ,exp) bit there. But if you uncomment the
> newlines, well, you return whatever newline returns, which is actually
> unspecified.
Thanks!
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2009-11-16 17:40 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-15 16:33 Need help with a macro Josef Wolf
2009-11-15 18:18 ` Andy Wingo
2009-11-16 17:40 ` Josef Wolf
2009-11-15 20:26 ` Neil Jerram
2009-11-16 17:20 ` Josef Wolf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).