From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: duthen.cnv@gmail.com Newsgroups: gmane.emacs.help Subject: Re: 'length' function for lists and cons cells? Date: Mon, 25 Mar 2013 08:24:05 -0700 (PDT) Message-ID: <54eefda1-7c85-4c18-8c02-5376dde008e0@googlegroups.com> References: <87620k7gwm.fsf@gmail.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 1364236542 5088 80.91.229.3 (25 Mar 2013 18:35:42 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 25 Mar 2013 18:35:42 +0000 (UTC) Cc: help-gnu-emacs@gnu.org, Thorsten Jolitz To: gnu.emacs.help@googlegroups.com Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Mar 25 19:36:08 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 1UKCFb-0000dU-CK for geh-help-gnu-emacs@m.gmane.org; Mon, 25 Mar 2013 19:36:07 +0100 Original-Received: from localhost ([::1]:57490 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UKCFC-0003za-Ss for geh-help-gnu-emacs@m.gmane.org; Mon, 25 Mar 2013 14:35:43 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:40310) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UK9Fq-0003KX-3h for help-gnu-emacs@gnu.org; Mon, 25 Mar 2013 11:24:16 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UK9Fn-0000dn-1p for help-gnu-emacs@gnu.org; Mon, 25 Mar 2013 11:24:10 -0400 Original-Received: from mail-qe0-f56.google.com ([209.85.128.56]:40404) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UK9Fm-0000dg-UR for help-gnu-emacs@gnu.org; Mon, 25 Mar 2013 11:24:06 -0400 Original-Received: by mail-qe0-f56.google.com with SMTP id nd7so2202841qeb.11 for ; Mon, 25 Mar 2013 08:24:06 -0700 (PDT) X-Received: by 10.49.49.193 with SMTP id w1mr678711qen.3.1364225045936; Mon, 25 Mar 2013 08:24:05 -0700 (PDT) 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=Rwf09woAAAAhc6wyfl577hCCwQ8gnJCm 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.128.56 X-Mailman-Approved-At: Mon, 25 Mar 2013 14:35:21 -0400 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:89742 Archived-At: Le jeudi 21 mars 2013 21:10:10 UTC+1, Drew Adams a =E9crit=A0: > > which function could I use when I map an alist e.g. with dolist, > > that contains both types of associations as shown below: cons=20 > > cells, or lists with 3 or more elements? > > 'length' doesn't work on cons cells: >=20 > > Debugger entered--Lisp error: (wrong-type-argument listp "c") > > length(("a" . "c")) >=20 > It's not clear to me what you're asking. >=20 > 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? >=20 > If so then it is up to you to define what you want such a "length" to be/= mean. >=20 > Perhaps what you want is something like this? >=20 > (defun thorsten-len (xs) > (cond ((null xs) 0) > ((null (cdr xs)) 1) > ((atom (cdr xs)) 2) > (t (1+ (thorsten-len (cdr xs)))))) >=20 > (thorsten-len '(1 2 3 4 . 5)) =3D> 5 >=20 > (setq foo '((a . 1) (b 2) (c (3 3 3)) (d 4 4 4 . 4))) > (mapcar #'thorsten-len foo) =3D> (2 2 2 5) >=20 > (setq bar ()) > (dolist (ff foo) (push ff bar)) > (reverse bar) =3D> ((a . 1) (b 2) (c (3 3 3)) (d 4 4 4 . 4)) Considering that: (a b) contains 2 cons cells and 2 values (a and b), (a b . c) contains 2 cons cells and 3 values (a b and c), (a b c) contains 3 cons cells and 3 values (a b and c). So, one possible point of vue (not necessarily mine, though!)=20 could consider that (a b . c) is "a little bit longer" than (a b)=20 and "a little bit shorter" than (a b c)! Hence, the function: (defun semi-length (xs) (cond ((null xs) 0) ((atom xs) .5) (t (1+ (semi-length (cdr xs)))))) (mapcar (lambda (x) (cons (semi-length x) x)) '(() a (a) (a . b) (a b) (a b . c) (a b c))) ((0) (0.5 . a) (1 a) (1.5 a . b) (2 a b) (2.5 a b . c) (3 a b c)) A true list is just one whith an integer semi-length whereas a dotted-paired list is one with a fractional semi-length ! :)