From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: jack-mac Newsgroups: gmane.emacs.help Subject: Re: "defmacro" and "local variables" and "let" and "nconc" strange behaviour... Date: Mon, 14 Jan 2013 01:04:47 -0800 (PST) Message-ID: <13970288-575d-4593-a694-0c3f09c3844b@googlegroups.com> References: <87k3rh6uez.fsf@gavenkoa.example.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1358154304 1566 80.91.229.3 (14 Jan 2013 09:05:04 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 14 Jan 2013 09:05:04 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: gnu.emacs.help@googlegroups.com Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Jan 14 10:05:22 2013 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Tufyr-0005Mf-UN for geh-help-gnu-emacs@m.gmane.org; Mon, 14 Jan 2013 10:05:22 +0100 Original-Received: from localhost ([::1]:54595 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tufyb-0000f2-JF for geh-help-gnu-emacs@m.gmane.org; Mon, 14 Jan 2013 04:05:05 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:50728) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TufyS-0000Yj-LV for help-gnu-emacs@gnu.org; Mon, 14 Jan 2013 04:04:59 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TufyL-0007im-MZ for help-gnu-emacs@gnu.org; Mon, 14 Jan 2013 04:04:56 -0500 Original-Received: from mail-vb0-f61.google.com ([209.85.212.61]:34489) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TufyL-0007ig-Iy for help-gnu-emacs@gnu.org; Mon, 14 Jan 2013 04:04:49 -0500 Original-Received: by mail-vb0-f61.google.com with SMTP id fs19so2516817vbb.6 for ; Mon, 14 Jan 2013 01:04:48 -0800 (PST) Original-Received: by 10.49.71.135 with SMTP id v7mr15444173qeu.28.1358154287951; Mon, 14 Jan 2013 01:04:47 -0800 (PST) Original-Path: glegroupsg2000goo.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=193.49.124.107; posting-account=OWLhBgoAAAD1H7ELDXVfr3-5BJaMOe1v Original-NNTP-Posting-Host: 193.49.124.107 User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 193.49.124.107 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.212.61 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:88604 Archived-At: Le dimanche 13 janvier 2013 15:18:46 UTC+1, Oleksandr Gavenko a =E9crit=A0: > On 2013-01-13, Oleksandr Gavenko wrote: >=20 >=20 >=20 > > I wrote simple macro: >=20 > > >=20 > > (defmacro my-filter (pred list) >=20 > > "Construct list with elements from LIST which satisfy PRED." >=20 > > (let ( (r (make-symbol "r_")) ) >=20 > > `(let ( (,r '(nil)) ) >=20 > > (mapc (lambda (item) >=20 > > (when (,pred item) >=20 > > (nconc ,r (cons item nil)))) >=20 > > ,list) >=20 > > (cdr ,r)))) >=20 > > >=20 > > When I evaluate several times: >=20 > > >=20 > > (my-filter (lambda (x) x) '(1 nil "a")) >=20 > > >=20 > > I get sequentially: >=20 > > >=20 > > (1 "a") >=20 > > (1 "a" 1 "a") >=20 > > (1 "a" 1 "a" 1 "a") >=20 > > (1 "a" 1 "a" 1 "a" 1 "a") >=20 > > >=20 > > When I eval: >=20 > > >=20 > > (pp (macroexpand '(my-filter (lambda (x) x) '(1 nil "a")))) >=20 > > >=20 > > I get: >=20 > > >=20 > > (let >=20 > > ((r_ >=20 > > '(nil 1 "a" 1 "a" 1 "a" 1 "a" 1 "a" 1 "a" 1 "a" 1 "a"))) >=20 > > (mapc >=20 > > (lambda >=20 > > (item) >=20 > > (when >=20 > > ((lambda >=20 > > (x) >=20 > > x) >=20 > > item) >=20 > > (nconc r_ >=20 > > (cons item nil)))) >=20 > > '(1 nil "a")) >=20 > > (cdr r_)) >=20 > > >=20 > > Why instead of '(nil) I get something else? >=20 >=20 >=20 > I found fix by changing '(nil) to (list nil). Please explain why first va= riant >=20 > fail? >=20 >=20 >=20 > --=20 >=20 > Best regards! To understand it, just try: (defun foo (x) (let ((l '(0))) (nconc l (list x)))) (foo 1) (foo 2) This looks like a closure!