From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Drew Adams" Newsgroups: gmane.emacs.help Subject: RE: Wanted: mapcar() without nils Date: Sat, 13 Jun 2009 13:02:54 -0700 Message-ID: <41D3998CCF604FD8ABAFBC9B152DC594@us.oracle.com> References: <65652541-cb7d-4c93-8baf-cd963990e57e@g1g2000yqh.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1244923425 20597 80.91.229.12 (13 Jun 2009 20:03:45 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 13 Jun 2009 20:03:45 +0000 (UTC) To: "=?iso-8859-1?Q?'Nordl=F6w'?=" , Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Jun 13 22:03:41 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1MFZS0-0002Gn-Nt for geh-help-gnu-emacs@m.gmane.org; Sat, 13 Jun 2009 22:03:41 +0200 Original-Received: from localhost ([127.0.0.1]:39689 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MFZS0-0001hf-B6 for geh-help-gnu-emacs@m.gmane.org; Sat, 13 Jun 2009 16:03:40 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MFZRX-0001cn-8K for help-gnu-emacs@gnu.org; Sat, 13 Jun 2009 16:03:11 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MFZRS-0001am-8M for help-gnu-emacs@gnu.org; Sat, 13 Jun 2009 16:03:10 -0400 Original-Received: from [199.232.76.173] (port=53014 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MFZRS-0001aa-2N for help-gnu-emacs@gnu.org; Sat, 13 Jun 2009 16:03:06 -0400 Original-Received: from acsinet11.oracle.com ([141.146.126.233]:60492) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1MFZRR-0002XG-Dp for help-gnu-emacs@gnu.org; Sat, 13 Jun 2009 16:03:05 -0400 Original-Received: from rgminet15.oracle.com (rcsinet15.oracle.com [148.87.113.117]) by acsinet11.oracle.com (Switch-3.3.1/Switch-3.3.1) with ESMTP id n5DK3tVs012199 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sat, 13 Jun 2009 20:03:56 GMT Original-Received: from abhmt002.oracle.com (abhmt002.oracle.com [141.146.116.11]) by rgminet15.oracle.com (Switch-3.3.1/Switch-3.3.1) with ESMTP id n5DK34NQ017270; Sat, 13 Jun 2009 20:03:04 GMT Original-Received: from dradamslap1 (/141.144.88.237) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Sat, 13 Jun 2009 13:02:58 -0700 X-Mailer: Microsoft Office Outlook 11 In-Reply-To: <65652541-cb7d-4c93-8baf-cd963990e57e@g1g2000yqh.googlegroups.com> Thread-Index: AcnsVxQUHJ4kg+lzSL+lMIndAZGe9QAB2AUQ X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579 X-Source-IP: abhmt002.oracle.com [141.146.116.11] X-Auth-Type: Internal IP X-CT-RefId: str=0001.0A010204.4A3405F2.01DB:SCFSTAT5015188,ss=1,fgs=0 X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 1) X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:65265 Archived-At: > (defun extract-elements (pred seq) > (delq nil (mapcar `(lambda (elm) > (when (,pred elm)) elm) > seq))) > > But why does my test-cases return '(a b 1 2) all the time? The body of your lambda is this: (when X) Y The body is an implicit progn. It returns the value of the last sexp, which is Y. You meant (when X Y), not (when X) Y. Better yet, you meant (and X Y), since you are interested in the value returned - you are not interested only in some side effect. (defun extract-elements (pred seq) (delq nil (mapcar (lambda (elm) (and (funcall pred elm) elm)) seq))) Or instead of constructing a list and then filtering it, construct it with only the elements you want. IOW, promote the filter to be inside the iteration/map. (defun extract-elements (pred seq) (let ((r ())) (dolist (e seq) (when (funcall pred e) (push e r))) (nreverse r))) That's not better or worse - just a different style (in this case). You can debug problems like this yourself, by using `M-x debug-on-entry extract-elements'. Or try evaluating parts of your code using your concrete test args: (mapcar (lambda (elm) (when (symbolp elm)) elm) '(a b 1 2)) (progn (when (symbolp 1)) 1) Doing that, you see right away that the problem is a misplaced paren, not something particular to `delq' or `mapcar' or backquote syntax or... IOW, simplify the problem.