help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 'length' function for lists and cons cells?


From: Thorsten Jolitz
Subject: Re: 'length' function for lists and cons cells?
Date: Fri, 22 Mar 2013 03:38:11 +0100
User-agent: Gnus/5.130002 (Ma Gnus v0.2) Emacs/24.2 (gnu/linux)

"Pascal J. Bourguignon" <pjb@informatimago.com> writes:

> I don't understand what you want to do.

well, avoid the errors I get when mapping alists with true lists and
cons cells as elements with functions like 'lenght of 'cdr etc. 

> You can use length and dolist on alists and it will work perfectly:
> (length '((a . 1) (b . 2) (c . 3)))
> --> 3

yes, but, when mapping the elements: 

,------------------------------------------------------------
| (length '(a . 1))
| 
| Debugger entered--Lisp error: (wrong-type-argument listp 1)
|   length((a . 1))
`------------------------------------------------------------

> (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

I wasn't aware of destructuring-bind, thats cl stuff - had to look it up here:
http://dto.github.com/notebook/require-cl.html#sec-6-6

> 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

ok, thanks for the hint. 

-- 
cheers,
Thorsten




reply via email to

[Prev in Thread] Current Thread [Next in Thread]