Re: A macro and an unwanted containing list in the resulting form

From:

Sebastian Tennant

Subject:

Re: A macro and an unwanted containing list in the resulting form

Date:

Thu, 24 May 2007 00:32:56 +0300

User-agent:

Gnus/5.110006 (No Gnus v0.6) Emacs/22.0.95 (gnu/linux)

Quoth Pascal Bourguignon <address@hidden>:
> So when you want to write s-exps, you can easily see what the result
> will be:
>
> (defmacro build-cond (alist)
> `(cond ,@(mapcar (lambda (each)
> `((equal my-var ,(car each)) ,(cdr each)))
> alist)))
Hmm, that's really good to know. It's certainly a lot more readable,
but I dread to think how this C-code must look (not that I'm a
C-coder), given that you have unquoted lists within a backquoted list
within an unspliced list within another backquoted list... It must be
a veritable spaghetti-junction, with a few chopped onions thrown in
for good measure!
> And never put a quote before a lambda expression!!! It works by
>chance in emacs lisp, but it prevents the compiler to compile the
>anonymous function, and it wouldn't work in other lisps.
Noted, although presumably you mean you shouldn't _directly_ quote a
lambda expression? In the above code, the lambda expression occurs
within an unspliced list, itself within a backquoted list... Ah, I
suppose this has the effect of the lambda expression not being quoted
at all?
> Also, it would be better if you hadn't magic variable names in your
> macro. Instead of using my-var, let the user of the macro decide what
> variable name should be tested. You could also use a better name
> than build-cond; for example, string-case. And let's improve a little
> the syntax, removing one layer of useless parentheses, getting the
> list of clauses from the &rest of the arguments:
>
> (defmacro string-case (var-name &rest alist)
> `(cond ,@(mapcar (lambda (each)
> `((equal ,var-name ,(car each)) ,(cdr each)))
> alist)))
>
> So now you can write:
>
> (string-case e
> ("hello" . (message "hi"))
> ("goodbye" . (message "bye")))
I understand your point, but I don't think it is necessary in this
case.
I'm writing a wrapper function to make-comint-in-buffer that also sets
up a simple process sentinel given the ("string" . FORM) alist. The
"string" is matched against the process event ("finished" or "killed",
for example) so the variable is always 'ev' (or whatever variable I
choose to hard-code in the process sentinel's lambda function).
> But if you write:
>
> (string-case (concat "hel" "lo")
> ("hello" . (message "hi"))
> ("goodbye" . (message "bye")))
>
> the expansion will evaluate several times (concat "hel" "lo"), which
> would be very bad when the expression has side effects. We need to
> take care of that in the macro, building a temporary and unique
> variable name with gensym, and binding it to the expression:
>
> (defmacro string-case (strexpr &rest alist)
> (let ((var-name (gensym)))
> `(let ((,var-name ,strexpr))
> (cond ,@(mapcar (lambda (each)
> `((equal ,var-name ,(car each)) ,(cdr each)))
> alist)))))
Hmm, I think I follow... just! Excuse me if I annotate what follows
for my benefit (and hopefully other's).
> And finally, it would be better if we could have several forms in each
> branches, and if we lost the dot:
"lost the dot" = "do without cons cells" -----+
|
+--------
|
\/
> (defmacro string-case (strexpr &rest alist)
> (let ((var-name (gensym)))
> `(let ((,var-name ,strexpr))
> (cond ,@(mapcar (lambda (each)
> `((equal ,var-name ,(car each)) ,@(cdr each)))
> alist))))) ^
|
I see. ----------------------------------------------------+--+
|
> (macroexpand '(string-case (aref my-strings (incf i)) |
> ("hello" (message "hi") (message "ho")) |
> ("goodbye" (message "bye")))) |
|
Unique variable name created by (gensym) ---+ |
| |
---------------------------------+ |
| |
\/ |
> (let ((G42298 (aref my-strings (incf i)))) |
> (cond |
> ((equal G42298 "hello") |
> (message "hi") (message "ho")) <--+---------------------+
> ((equal G42298 "goodbye") |
> (message "bye")))) |
|
"several forms in each branch" ----------+
Well Pascal, thank you for an excellent crash-course in macro
expansion!
I hope my annotations are correct, and not out of place, although
people not reading this in a fixed-with font will no doubt wonder what
on earth is going on, not to mention the other nasty things
line-wrapping news-manglers are inclined to do!
Sebastian