[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: 'length' function for lists and cons cells?
From: |
Pascal J. Bourguignon |
Subject: |
Re: 'length' function for lists and cons cells? |
Date: |
Thu, 21 Mar 2013 21:49:23 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux) |
Thorsten Jolitz <tjolitz@gmail.com> writes:
> Hi List,
>
> 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:
>
> ,------------------------
> | (length '("a" "b" "c"))
> | 3
> `------------------------
>
> ,--------------------------------------------------------------
> | (length '("a" . "c"))
> |
> | Debugger entered--Lisp error: (wrong-type-argument listp "c")
> | length(("a" . "c"))
> `--------------------------------------------------------------
I don't understand what you want to do.
You can use length and dolist on alists and it will work perfectly:
(length '((a . 1) (b . 2) (c . 3)))
--> 3
(dolist (entry '((a . 1) (b . 2) (c . 3)))
(destructuring-bind (key . value) entry
(insert (format "key = %S, value = %S\n" key value))))
key = a, value = 1
key = b, value = 2
key = c, value = 3
If you want to count the cons cells, then why stop at the dotted-lists?
There are also circular lists. See how you can do it at:
https://gitorious.org/com-informatimago/com-informatimago/blobs/master/common-lisp/cesarum/list.lisp#line303
(note: this is Common Lisp, some translation to emacs lisp is needed
(mostly, remove the colons from the loop keywords)).
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
- Re: 'length' function for lists and cons cells?,
Pascal J. Bourguignon <=
- Re: 'length' function for lists and cons cells?, Thorsten Jolitz, 2013/03/21
- Re: 'length' function for lists and cons cells?, Mark Skilbeck, 2013/03/22
- Re: 'length' function for lists and cons cells?, Thorsten Jolitz, 2013/03/22
- Message not available
- Re: 'length' function for lists and cons cells?, Pascal J. Bourguignon, 2013/03/23
- RE: 'length' function for lists and cons cells?, Drew Adams, 2013/03/23
- Message not available
- Re: 'length' function for lists and cons cells?, Pascal J. Bourguignon, 2013/03/23
- RE: 'length' function for lists and cons cells?, Drew Adams, 2013/03/24
Message not available
Message not available