From: Jim
Subject: macro expansion
Date: 
Message-ID: <1191868754.716618.107850@g4g2000hsf.googlegroups.com>
Hi. I'm a lisp noob and could use some help. Using slime - lispbox.

(defvar *foo* (make-hash-table :test 'equal))
*foo*  --> shows 0 rows

(defmacro boo (you)
	   `(setf (gethash "1" *foo*) ,you)
	   `(setf (gethash "2" *foo*) ,you))

(boo "hoo")

*foo* --> shows 1 row, just the last one

(gethash "1" *foo*)
(gethash "2" *foo*)


Why does the first set hash not take?
With macroexpansion it looks to me like the first line should execute.
Any thoughts?

Jim

From: ···········@gmail.com
Subject: Re: macro expansion
Date: 
Message-ID: <1191872194.264893.28760@r29g2000hsg.googlegroups.com>
On 8 Pa , 20:39, Jim <·········@gmail.com> wrote:
> Hi. I'm a lisp noob and could use some help. Using slime - lispbox.
>
> (defvar *foo* (make-hash-table :test 'equal))
> *foo*  --> shows 0 rows
>
> (defmacro boo (you)
>            `(setf (gethash "1" *foo*) ,you)
>            `(setf (gethash "2" *foo*) ,you))
>
> (boo "hoo")
>
> *foo* --> shows 1 row, just the last one
>
> (gethash "1" *foo*)
> (gethash "2" *foo*)
>
> Why does the first set hash not take?
> With macroexpansion it looks to me like the first line should execute.
> Any thoughts?
>
> Jim

(defmacro boo (you)
  `(progn (setf (gethash "1" *foo*) ,you)
	  (setf (gethash "2" *foo*) ,you)))

CL-USER> (macroexpand '(boo "cat"))
(PROGN (SETF (GETHASH "1" *FOO*) "cat") (SETF (GETHASH "2" *FOO*)
"cat"))
T

-or-

(defmacro boo (you)
  `(setf (gethash "1" *foo*) ,you (gethash "2" *foo*) ,you))

You just have to return one thing (possibly using prog1/n/wtf) - first
setf in your's macro is 'lost', only second is returned.

-
P.Z.
From: Jim
Subject: Re: macro expansion
Date: 
Message-ID: <1191874995.136689.70980@19g2000hsx.googlegroups.com>
Ahh! That helps. Thanks for responding Ken and P.Z.
From: Pillsy
Subject: Re: macro expansion
Date: 
Message-ID: <1191901995.288302.121350@19g2000hsx.googlegroups.com>
On Oct 8, 4:23 pm, Jim <·········@gmail.com> wrote:
> Ahh! That helps. Thanks for responding Ken and P.Z.

BTW, I noticed you were using SLIME, which makes macroexpansion
particularly easy. You can do it by placing the cursor on the first
"(" of the form you want to expand and hitting C-c RET.

Cheers,
Pillsy
From: Ken Tilton
Subject: Re: macro expansion
Date: 
Message-ID: <5UuOi.57$UF3.10@newsfe12.lga>
Jim wrote:
> Hi. I'm a lisp noob and could use some help. Using slime - lispbox.

You are doomed.

> 
> (defvar *foo* (make-hash-table :test 'equal))
> *foo*  --> shows 0 rows
> 
> (defmacro boo (you)
> 	   `(setf (gethash "1" *foo*) ,you)
> 	   `(setf (gethash "2" *foo*) ,you))
> 
> (boo "hoo")
> 
> *foo* --> shows 1 row, just the last one
> 
> (gethash "1" *foo*)
> (gethash "2" *foo*)
> 
> 
> Why does the first set hash not take?

The compiler never sees it, so it cannot run which makes it hard to "take".

It never gets seen because if you had (defun xxx () 1 2 3) 3 would be 
returned.

> With macroexpansion it looks to me like the first line should execute.

Could you post the expansion and the details of your environment? Like, 
what Lisp does Lispbox install?


ken
From: Jim
Subject: Re: macro expansion
Date: 
Message-ID: <1191870500.389020.267890@57g2000hsv.googlegroups.com>
Sure, I'm running SBCL 1.0.3 in emacs on ubuntu linux.
http://www.gigamonkeys.com/lispbox/


(EVAL-WHEN (:COMPILE-TOPLEVEL :LOAD-TOPLEVEL :EXECUTE)
  (SB-C::%DEFMACRO 'BOO
                   #'(SB-INT:NAMED-LAMBDA (DEFMACRO BOO)
                                          (#:WHOLE2057
#:ENVIRONMENT2058)
                                          (DECLARE (IGNORE
#:ENVIRONMENT2058))
                                          (LET* ()
                                            (LET ((#:ARGS2060
                                                   (CDR #:WHOLE2057)))
                                              (UNLESS
                                                  (SB-INT:PROPER-LIST-
OF-LENGTH-P
                                                   #:ARGS2060 1 1)
                                                (SB-KERNEL::ARG-COUNT-
ERROR
                                                 'DEFMACRO 'BOO
#:ARGS2060
                                                 '(YOU) 1 1)))
                                            (LET* ((YOU
                                                    (CAR (CDR
#:WHOLE2057))))
                                              (BLOCK BOO
                                                `(SETF (GETHASH "1"
*FOO*)
                                                         ,YOU)
                                                '(SETF (GETHASH "2"
*FOO*))))))
                   '(YOU) NIL '(MACRO-FUNCTION BOO)))
From: Ken Tilton
Subject: Re: macro expansion
Date: 
Message-ID: <%nvOi.73$S67.5@newsfe12.lga>
Jim wrote:
> Sure, I'm running SBCL 1.0.3 in emacs on ubuntu linux.
> http://www.gigamonkeys.com/lispbox/
> 
> 
> (EVAL-WHEN (:COMPILE-TOPLEVEL :LOAD-TOPLEVEL :EXECUTE)
>   (SB-C::%DEFMACRO 'BOO
>                    #'(SB-INT:NAMED-LAMBDA (DEFMACRO BOO)
>                                           (#:WHOLE2057
> #:ENVIRONMENT2058)
>                                           (DECLARE (IGNORE
> #:ENVIRONMENT2058))
>                                           (LET* ()
>                                             (LET ((#:ARGS2060
>                                                    (CDR #:WHOLE2057)))
>                                               (UNLESS
>                                                   (SB-INT:PROPER-LIST-
> OF-LENGTH-P
>                                                    #:ARGS2060 1 1)
>                                                 (SB-KERNEL::ARG-COUNT-
> ERROR
>                                                  'DEFMACRO 'BOO
> #:ARGS2060
>                                                  '(YOU) 1 1)))
>                                             (LET* ((YOU
>                                                     (CAR (CDR
> #:WHOLE2057))))
>                                               (BLOCK BOO
>                                                 `(SETF (GETHASH "1"
> *FOO*)
>                                                          ,YOU)
>                                                 '(SETF (GETHASH "2"
> *FOO*))))))
>                    '(YOU) NIL '(MACRO-FUNCTION BOO)))
> 
> 

I see. What happens when you macroexpand (boo "hoo")? That was what I 
thought you meant by "with macroexpansion it looks like...".

If PCL recommended using macroexpansion to debug macros (an excellent 
suggestion) it means of your /application/ of your macro (again, of (boo 
"hoo"), not of the defmacro form in which you define your macro.

At that point my earlier reply cuts in -- Lisp forms return the last 
subform evaluated unless you start messing with prog1 and friends.

hth, kt

-- 
http://www.theoryyalgebra.com/

"We are what we pretend to be." -Kurt Vonnegut