Page 1 of 1

Define-macro & setf/$it

Posted: Wed Nov 12, 2008 7:25 pm
by newdep
Hi Lutz,

Why is this behaviour?

'setf does evaluate the expression, so inside the macro
i would expect this to work. but i dont understand why not..
When I replace $it with (args 0) it also doesnt work..
..Probably a very logical explanation here..


-> (define-macro (z) (setf (args 0) (string $it)) )
(lambda-macro () (setf (args 0) (string $it)))
-> (z hello world)

ERR: no symbol reference found
->



Edit: Is it because (args) does not refer ? But i would expect this to be handled like a list..

Posted: Wed Nov 12, 2008 7:34 pm
by Lutz
'setf' refers to a place not a symbol like the old 'set', so when you pass a symbol via a macro you have to use 'eval':

Code: Select all

(define-macro (z) (setf (eval (args 0)) (string $it)) )
(setq hello 123)

(z hello) => "123"

hello => "123"

Posted: Wed Nov 12, 2008 7:45 pm
by newdep
oke clear... now look here.. do you notice someting ? -->

-> (define-macro (z) (push (string (pop (args))) (args)) )
(lambda-macro () (push (string (pop (args))) (args)))
-> (z 'hello 'world)
("'hello" 'hello 'world)


The first 'hello became " 'hello " ... ?

while a (string 'hello) => "hello" and not " 'hello "

EDIT:
Im a little lost... also the fact that the (args) now has a double hello
as where i destructively popped it and pushed it..
So the 'pop returns the unchanged but the push is allowed)


EDIT2:
I just got a $.25c dropp ;-) thanks..got it..

Posted: Wed Nov 12, 2008 7:59 pm
by Lutz
- you see quotes, because you are using 'define-macro' and you are passing quoted arguments

- (args) always returns a copy you cannot change args itself (which is a good thing ;-)). But you could do the following:

Code: Select all

(define-macro (z) 
	(let (params (args))
	(push (string (pop params)) params) ))

(z hello world) => ("hello" world)