help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "defmacro" and "local variables" and "let" and "nconc" strange behav


From: Barry Margolin
Subject: Re: "defmacro" and "local variables" and "let" and "nconc" strange behaviour...
Date: Sun, 13 Jan 2013 09:21:08 -0500
User-agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)

In article <address@hidden>,
 Oleksandr Gavenko <address@hidden> wrote:

> I wrote simple macro:
> 
>   (defmacro my-filter (pred list)
>     "Construct list with elements from LIST which satisfy PRED."
>     (let ( (r (make-symbol "r_")) )
>       `(let ( (,r '(nil)) )
>          (mapc (lambda (item)
>                  (when (,pred item)
>                    (nconc ,r (cons item nil))))
>                ,list)
>          (cdr ,r))))
> 
> When I evaluate several times:
> 
>   (my-filter (lambda (x) x) '(1 nil "a"))
> 
> I get sequentially:
> 
>   (1 "a")
>   (1 "a" 1 "a")
>   (1 "a" 1 "a" 1 "a")
>   (1 "a" 1 "a" 1 "a" 1 "a")
> 
> When I eval:
> 
>   (pp (macroexpand '(my-filter (lambda (x) x) '(1 nil "a"))))
> 
> I get:
> 
>   (let
>       ((r_
>         '(nil 1 "a" 1 "a" 1 "a" 1 "a" 1 "a" 1 "a" 1 "a" 1 "a")))
>     (mapc
>      (lambda
>        (item)
>        (when
>            ((lambda
>               (x)
>               x)
>             item)
>          (nconc r_
>                 (cons item nil))))
>      '(1 nil "a"))
>     (cdr r_))
> 
> Why instead of '(nil) I get something else?

Because nconc is destructively modifying its own source code.

-- 
Barry Margolin, address@hidden
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


reply via email to

[Prev in Thread] Current Thread [Next in Thread]