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: 'length' function for lists and cons cells? Date: Thu, 21 Mar 2013 13:10:10 -0700 Message-ID: <7189477CE70041E985830EA4A0075ED7@us.oracle.com> References: <87620k7gwm.fsf@gmail.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1363896631 29203 80.91.229.3 (21 Mar 2013 20:10:31 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 21 Mar 2013 20:10:31 +0000 (UTC) To: "'Thorsten Jolitz'" , Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Mar 21 21:10:53 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 1UIlp7-0003ga-0f for geh-help-gnu-emacs@m.gmane.org; Thu, 21 Mar 2013 21:10:53 +0100 Original-Received: from localhost ([::1]:39957 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UIloj-0006JR-33 for geh-help-gnu-emacs@m.gmane.org; Thu, 21 Mar 2013 16:10:29 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:37020) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UIloY-0006JJ-FZ for help-gnu-emacs@gnu.org; Thu, 21 Mar 2013 16:10:19 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UIloW-0005QF-CG for help-gnu-emacs@gnu.org; Thu, 21 Mar 2013 16:10:18 -0400 Original-Received: from userp1040.oracle.com ([156.151.31.81]:23765) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UIloW-0005PT-6P for help-gnu-emacs@gnu.org; Thu, 21 Mar 2013 16:10:16 -0400 Original-Received: from ucsinet21.oracle.com (ucsinet21.oracle.com [156.151.31.93]) by userp1040.oracle.com (Sentrion-MTA-4.3.1/Sentrion-MTA-4.3.1) with ESMTP id r2LKACRs009939 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 21 Mar 2013 20:10:13 GMT Original-Received: from acsmt357.oracle.com (acsmt357.oracle.com [141.146.40.157]) by ucsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id r2LKABlH023765 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 21 Mar 2013 20:10:12 GMT Original-Received: from abhmt106.oracle.com (abhmt106.oracle.com [141.146.116.58]) by acsmt357.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id r2LKABMf026631; Thu, 21 Mar 2013 15:10:11 -0500 Original-Received: from dradamslap1 (/130.35.178.8) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 21 Mar 2013 13:10:11 -0700 X-Mailer: Microsoft Office Outlook 11 In-Reply-To: <87620k7gwm.fsf@gmail.com> Thread-Index: Ac4mYPUIaFG/CryVR1qtG3hszXexiAAACO0g X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6157 X-Source-IP: ucsinet21.oracle.com [156.151.31.93] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 156.151.31.81 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:89639 Archived-At: > which function could I use when I map an alist e.g. with dolist, that > contains both types of associations as shown below: cons > cells, or lists with 3 or more elements? > > 'length' doesn't work on cons cells: > > Debugger entered--Lisp error: (wrong-type-argument listp "c") > length(("a" . "c")) It's not clear to me what you're asking. Are you looking for a "length" function that works for both true lists (but why do you mention 3 or more elements?) and a cons whose last cdr is a non-nil atom? If so then it is up to you to define what you want such a "length" to be/mean. Perhaps what you want is something like this? (defun thorsten-len (xs) (cond ((null xs) 0) ((null (cdr xs)) 1) ((atom (cdr xs)) 2) (t (1+ (thorsten-len (cdr xs)))))) (thorsten-len '(1 2 3 4 . 5)) => 5 (setq foo '((a . 1) (b 2) (c (3 3 3)) (d 4 4 4 . 4))) (mapcar #'thorsten-len foo) => (2 2 2 5) (setq bar ()) (dolist (ff foo) (push ff bar)) (reverse bar) => ((a . 1) (b 2) (c (3 3 3)) (d 4 4 4 . 4))