[gambit-list] Problem using 'if' in a macro to conditionalydefinea let form

Logan, Patrick D patrick.d.logan at intel.com
Tue Mar 29 21:04:05 EST 2005


Ah, OK. Because the UNQUOTE within the LET bindings has to return
something, and there is no "else" clause in the IF, you get by default a
#!VOID.

Instead rewrite the IF to generate both bindings in the LET. Instead of
UNQUOTE, use UNQUOTE-SPLICING...

`(let (,@(if (is-cool? somestuff)
             `((x ,somestuff)
               (z do-something-else-cool))
             `((x ,somestuff))))
    ...)

That should be close, plus or minus a typo or two.

-Patrick



-----Original Message-----
From: gambit-list-bounces at iro.umontreal.ca
[mailto:gambit-list-bounces at iro.umontreal.ca] On Behalf Of Eric Merritt
Sent: Tuesday, March 29, 2005 5:50 PM
To: gambit-list at iro.umontreal.ca
Subject: Re: [gambit-list] Problem using 'if' in a macro to
conditionalydefinea let form

see inline ->

On Tue, 29 Mar 2005 17:16:29 -0800, Logan, Patrick D
<patrick.d.logan at intel.com> wrote:
> Not sure what you really want to have expanded and what you want in
the
> result. I have found it helpful to begin with the end in mind.

 (define-macro (foo somestuff)
  `(let ((x ,sumstuff)
         ,(if (is-cool sumstuff)
             `(z (do-something-else-cool))))
      (some-more-stuff))

So this would preferably expand in two ways depending on what 'sumstuff'
is

  (let ((x avalue)
        (z (do-something-else-cool)))
      (some-more-stuff))

or

 (let ((x avalue))
      (some-more-stuff))

However, the second option is impossible because the above macro will
expand to
 
 (let ((x avalue)
        #!void)
      (some-more-stuff)))

in the face of an uncool 'sumstuff'. At least thats what it looks like
is happening when I expand the macro.

> Assuming you want the initial form to look like...
> 
> (FOO SOMESTUFF)
> 
> Can you provide an example, i.e. replace SOMESTUFF with some actual
> stuff.

 Ok simple enough. So lets say sumstuff is

  '((cool 33) (not-cool 44))

 and is-cool is defined as

(define (is-cool lst)
   (cond
     ((null? lst) #f)
     ((equal? 'cool (caar lst)) #t)
     (else (is-cool (cdr lst)))))


> 
> Then for this example, what do you want the final form to look like
> after expansion?
 
  (let ((x avalue)
        (z (do-something-else-cool)))
      (some-more-stuff))

or 

  (let ((x avalue))
      (some-more-stuff))

depending on the value of sumstuff


> Now defining the macro is a simple matter of programming. Outside of
the
> define-macro, you can play with quasiquotes to get just the data
looking
> the way you want it.

 Unfortunately, unless I am missing something big I can't for reasons
stated above. Because although this


  (let ((x avalue)
        (z (do-something-else-cool)))
      (some-more-stuff))

can be evaluated to just fine. I cannot get this


  (let ((x avalue))
      (some-more-stuff))

to result. In instances where sumstuff is (is-cool sumstuff) evaluates
to false I get an ill-formed let error.
_______________________________________________
Gambit-list mailing list
Gambit-list at iro.umontreal.ca
http://mailman.iro.umontreal.ca/mailman/listinfo/gambit-list



More information about the Gambit-list mailing list