Define-macro & setf/$it

Q&A's, tips, howto's
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Define-macro & setf/$it

Post 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..
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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"

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post 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..
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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)

Locked