mystery quoting

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

Post by Lutz »

Here an explanation again for the quote-effect, this time in a different way:

The 'qq' macro is basically a list maker, taking un-evaluated arguments, while 'list' is a list maker evaluating arguments:

Code: Select all

> (define-macro (qq) (args))

> (list 'a 'b 'c)
(a b c)
> (qq a b c)
(a b c)
If you agree on the outcome of above examples, you have to agree upon the following behavior of 'qq' too.

Code: Select all

> (apply list '('a 'b 'c))
('a 'b 'c)
> (apply qq '(a b c))
('a 'b 'c)
If the applied 'qq' whould not quote the list members it would behave like 'list', which it is not.


Lutz

nikus80
Posts: 16
Joined: Tue Nov 21, 2006 2:19 pm
Location: argentina

Post by nikus80 »

I see what you mean.

(apply list '('a 'b 'c)) is equivalent to (list ''a ''b ''c)
so,
(apply qq '(a b c)) is equivalent to (qq 'a 'b 'c)

the thing is, I can't get rid of the quotes! with list, I can.

(apply list (list a b c)) is equivalent to (list a b c)
(apply qq (list a b c)) is not equivalent to (qq a b c)

In the first one, (list a b c) is evaled, then the resulting list is applied with list, without evaling its arguments since I've already evaled them. This works fine, since list is a function, and all it's arguments are evaled anyway. It works with any function btw.
Now, with apply qq, it isn't very straightforward. Cause (apply qq (list a b c)) is meant to work with functions only, that's what I meant early when I said applying a macro just felt weird. With apply, I can't get something that translates to (qq a b c). so the answer to "We haven't yet found, for my-or, a tail call idiom involving apply which works." is guess is: we won't.

The thing is, it does not matters. Just use fanda's idiom. Or better, make it a macro or something.

at first I thought:
(define-macro (fapply)
(eval (cons (args 0) (eval (args 1)))))

but it doesn't works. I think is because

(define-macro (my-or)
(let (v (eval (args 0)))
(if v v (fapply my-or (rest (args))))))

when fapply evals (rest (args)), it thinks args refers to fapply args, not my-or args. I don't know which is the workaround to that. But for the moment, since there is no need to quote the first argument, just use:

(define (fapply)
(eval (cons (args 0) (args 1))))

still, like I said, I'm not happy with applying macros unless it's inside another macro, which in that case I probably need fapply, not apply.

Locked