* Re: 'length' function for lists and cons cells? [not found] <mailman.22586.1363890109.855.help-gnu-emacs@gnu.org> @ 2013-03-21 20:49 ` Pascal J. Bourguignon 2013-03-22 2:38 ` Thorsten Jolitz [not found] ` <mailman.22618.1363919920.855.help-gnu-emacs@gnu.org> 0 siblings, 2 replies; 19+ messages in thread From: Pascal J. Bourguignon @ 2013-03-21 20:49 UTC (permalink / raw) To: help-gnu-emacs 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 {}. ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: 'length' function for lists and cons cells? 2013-03-21 20:49 ` 'length' function for lists and cons cells? Pascal J. Bourguignon @ 2013-03-22 2:38 ` Thorsten Jolitz 2013-03-22 7:27 ` Mark Skilbeck [not found] ` <mailman.22618.1363919920.855.help-gnu-emacs@gnu.org> 1 sibling, 1 reply; 19+ messages in thread From: Thorsten Jolitz @ 2013-03-22 2:38 UTC (permalink / raw) To: help-gnu-emacs "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 ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: 'length' function for lists and cons cells? 2013-03-22 2:38 ` Thorsten Jolitz @ 2013-03-22 7:27 ` Mark Skilbeck 2013-03-22 9:57 ` Thorsten Jolitz [not found] ` <mailman.22632.1363946271.855.help-gnu-emacs@gnu.org> 0 siblings, 2 replies; 19+ messages in thread From: Mark Skilbeck @ 2013-03-22 7:27 UTC (permalink / raw) To: Thorsten Jolitz; +Cc: help-gnu-emacs It just doesn't make sense to talk about the length of a dotted _pair_, a cons-cell: it has a car and a cdr. On Fri, Mar 22, 2013 at 03:38:11AM +0100, Thorsten Jolitz wrote: > "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. > ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: 'length' function for lists and cons cells? 2013-03-22 7:27 ` Mark Skilbeck @ 2013-03-22 9:57 ` Thorsten Jolitz [not found] ` <mailman.22632.1363946271.855.help-gnu-emacs@gnu.org> 1 sibling, 0 replies; 19+ messages in thread From: Thorsten Jolitz @ 2013-03-22 9:57 UTC (permalink / raw) To: help-gnu-emacs Mark Skilbeck <m@iammark.us> writes: > It just doesn't make sense to talk about the length of a dotted > _pair_, a cons-cell: it has a car and a cdr. yes, but length is only an example, its about mapping an alist with true lists and cons cells as elements, and in some situations it seems necessary to make a program act differently depending on what the element is that is processed. Here is another example: 1. cadr does the right thing ,------------------ | (cdr '("a" "1")) | ("1") | | (cadr '("a" "1")) | "1" `------------------ 2. cdr does the right thing, cadr gives an error ,-------------------------------------------------------------- | (cdr '("a" . "1")) | "1" | | (cadr '("a" . "1")) | | Debugger entered--Lisp error: (wrong-type-argument listp "1") | cadr(("a" . "1")) `-------------------------------------------------------------- So if both, true lists and cons cells, are processed in a 'dolist' or 'mapc', how do you get that second string element without writing something like ,---------------------------------- | (defun tj/act-conditional (lst) | (if (cdr (last lst)) | (cdr lst) | (cadr lst))) | | (tj/act-conditional '("a" . "1")) | "1" | | (tj/act-conditional '("a" "1")) | "1" `---------------------------------- otherwise: ,-------------------------------------------------------------- | (defun tj/act-blindly-1 (lst) (cdr lst)) | (defun tj/act-blindly-2 (lst) (cadr lst)) | | (tj/act-blindly-1 '("a" . "1")) | "1" | | (tj/act-blindly-2 '("a" . "1")) | | Debugger entered--Lisp error: (wrong-type-argument listp "1") | cadr(("a" . "1")) | | | (tj/act-blindly-1 '("a" "1")) | ("1") | | (tj/act-blindly-2 '("a" "1")) | "1" `-------------------------------------------------------------- -- cheers, Thorsten ^ permalink raw reply [flat|nested] 19+ messages in thread
[parent not found: <mailman.22632.1363946271.855.help-gnu-emacs@gnu.org>]
* Re: 'length' function for lists and cons cells? [not found] ` <mailman.22632.1363946271.855.help-gnu-emacs@gnu.org> @ 2013-03-23 12:19 ` Pascal J. Bourguignon 2013-03-23 15:35 ` Drew Adams [not found] ` <mailman.22706.1364052941.855.help-gnu-emacs@gnu.org> 0 siblings, 2 replies; 19+ messages in thread From: Pascal J. Bourguignon @ 2013-03-23 12:19 UTC (permalink / raw) To: help-gnu-emacs Thorsten Jolitz <tjolitz@gmail.com> writes: > So if both, true lists and cons cells, are processed in a 'dolist' or proper-lists, not true lists. They all are true lists, ie. cons cells or nil. > 'mapc', how do you get that second string element without writing > something like > > ,---------------------------------- > | (defun tj/act-conditional (lst) > | (if (cdr (last lst)) > | (cdr lst) > | (cadr lst))) > | > | (tj/act-conditional '("a" . "1")) > | "1" > | > | (tj/act-conditional '("a" "1")) > | "1" > `---------------------------------- This is like asking how do you get the second digit of 123 and "123" without writing something like: (defun second-digit (thing) (etypecase thing (integer (mod (truncate thing (expt 10 (truncate (1- (log thing 10))))) 10)) (string (- (aref thing 1) ?0)))) (second-digit 12345) --> 2 (second-digit "1234") --> 2 The point is that lisp is a typed programming language. If a function expects data of type proper-list, then you don't pass it circular lists or dotted lists or strings or numbers, or whatever else. If you want to write a function that takes a or type argument, then you have to do the type casing yourself, or use other functions that take the same or type. (defun second-element (thing) (check-type thing (or null cons)) (typecase thing (null nil) (cons (typecase (cdr thing) (cons (cadr thing)) (t (cdr thing)))))) (second-element '()) --> nil (second-element '(1)) --> nil (second-element '(1 2)) --> 2 (second-element '(1 2 3)) --> 2 (second-element '(1 2 . 3)) --> 2 (second-element '(1 . 2)) --> 2 (second-element "123") error: (wrong-type-argument (or null cons) "123" thing) It is up to you to define what type of data you want to work with. -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}. ^ permalink raw reply [flat|nested] 19+ messages in thread
* RE: 'length' function for lists and cons cells? 2013-03-23 12:19 ` Pascal J. Bourguignon @ 2013-03-23 15:35 ` Drew Adams [not found] ` <mailman.22706.1364052941.855.help-gnu-emacs@gnu.org> 1 sibling, 0 replies; 19+ messages in thread From: Drew Adams @ 2013-03-23 15:35 UTC (permalink / raw) To: 'Pascal J. Bourguignon', help-gnu-emacs > > So if both, true lists and cons cells, are processed in a... > > proper-lists, not true lists. > They all are true lists, ie. cons cells or nil. Maybe for some people. For others, "true list" and "proper list" mean the same thing. FWIW, Emacs itself calls such a beast a "true list". From (elisp) `Cons Cells': Also by convention, the CDR of the last cons cell in a list is `nil'. We call such a `nil'-terminated structure a "true list". In Emacs Lisp, the symbol `nil' is both a symbol and a list with no elements. For convenience, the symbol `nil' is considered to have `nil' as its CDR (and also as its CAR). Hence, the CDR of a true list is always a true list. The CDR of a nonempty true list is a true list containing all the elements except the first. If the CDR of a list's last cons cell is some value other than `nil', we call the structure a "dotted list"... In any case, it is the description/meaning that is most important, not the name, and there we agree. ^ permalink raw reply [flat|nested] 19+ messages in thread
[parent not found: <mailman.22706.1364052941.855.help-gnu-emacs@gnu.org>]
* Re: 'length' function for lists and cons cells? [not found] ` <mailman.22706.1364052941.855.help-gnu-emacs@gnu.org> @ 2013-03-23 15:52 ` Pascal J. Bourguignon 2013-03-24 5:20 ` Drew Adams 0 siblings, 1 reply; 19+ messages in thread From: Pascal J. Bourguignon @ 2013-03-23 15:52 UTC (permalink / raw) To: help-gnu-emacs "Drew Adams" <drew.adams@oracle.com> writes: >> > So if both, true lists and cons cells, are processed in a... >> >> proper-lists, not true lists. >> They all are true lists, ie. cons cells or nil. > > Maybe for some people. For others, "true list" and "proper list" mean the same > thing. FWIW, Emacs itself calls such a beast a "true list". Even emacs itself calls such beasts true lists: (typep '() 'list) --> t (typep '(1) 'list) --> t (typep '(1 . #1=(2 3 . #1#)) 'list) --> t (typep '(1 . 2) 'list) --> t >>From (elisp) `Cons Cells': > > Also by convention, the CDR of the last cons cell in a > list is `nil'. We call such a `nil'-terminated structure > a "true list". No, f[…] manual, you're dumbing down the language, which is patronizing and error inducing. -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}. ^ permalink raw reply [flat|nested] 19+ messages in thread
* RE: 'length' function for lists and cons cells? 2013-03-23 15:52 ` Pascal J. Bourguignon @ 2013-03-24 5:20 ` Drew Adams 0 siblings, 0 replies; 19+ messages in thread From: Drew Adams @ 2013-03-24 5:20 UTC (permalink / raw) To: 'Pascal J. Bourguignon', help-gnu-emacs > >> > So if both, true lists and cons cells, are processed in a... > >> > >> proper-lists, not true lists. > >> They all are true lists, ie. cons cells or nil. > > > > Maybe for some people. For others, "true list" and "proper > > list" mean the same thing. FWIW, Emacs itself calls such a > > beast a "true list". > > Even emacs itself calls such beasts true lists: No, it does not. > (typep '() 'list) --> t > (typep '(1) 'list) --> t > (typep '(1 . #1=(2 3 . #1#)) 'list) --> t > (typep '(1 . 2) 'list) --> t You're apparently grasping at straws. There is no mention of "true list" there. The Emacs implementation of `typep' just calls `listp' when you pass it `list', so of course it returns non-nil for any cons cell or nil. `typep' does not distinguish "true"/"proper" lists from dotted lists, so it is useless to appeal to it as an authority on the terminology to use to distinguish the two. In any case, `typep' in Emacs is just an emulation of `typep' in Common Lisp, which refers to Common Lisp types. `typep' does not (and should not) try to say anything about Emacs Lisp types. The Emacs doc uses only "true list" for what you called a "proper list". It never uses "proper list" (which is also an OK name for it, IMO). > >>From (elisp) `Cons Cells': > > > > Also by convention, the CDR of the last cons cell in a > > list is `nil'. We call such a `nil'-terminated structure > > a "true list". > > No, f[.] manual, you're dumbing down the language, which is > patronizing and error inducing. I have no idea what you mean by that. You insisted that this be called a "proper list" and not a "true list". Why? To me, personally, either of those terms would be appropriate to distinguish a nil-terminated list from a dotted list (with non-nil last cdr). The fact is that your correction of "true" to "proper" does not correspond to the terminology that Emacs itself uses, and it does not add anything to our understanding. That's all. If you want to pick nits then get the nits right or you just confuse people. The idea of help-gnu-emacs is to help folks. If you want to change the terminology used in the manual, try `M-x report-emacs-bug' or emacs-devel@gnu.org. ^ permalink raw reply [flat|nested] 19+ messages in thread
[parent not found: <mailman.22618.1363919920.855.help-gnu-emacs@gnu.org>]
* Re: 'length' function for lists and cons cells? [not found] ` <mailman.22618.1363919920.855.help-gnu-emacs@gnu.org> @ 2013-03-22 7:45 ` Barry Margolin 2013-03-23 11:31 ` Pascal J. Bourguignon 1 sibling, 0 replies; 19+ messages in thread From: Barry Margolin @ 2013-03-22 7:45 UTC (permalink / raw) To: help-gnu-emacs In article <mailman.22618.1363919920.855.help-gnu-emacs@gnu.org>, Thorsten Jolitz <tjolitz@gmail.com> wrote: > "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. Don't give dotted lists to functions that are supposed to map over them in the first place. -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: 'length' function for lists and cons cells? [not found] ` <mailman.22618.1363919920.855.help-gnu-emacs@gnu.org> 2013-03-22 7:45 ` Barry Margolin @ 2013-03-23 11:31 ` Pascal J. Bourguignon 1 sibling, 0 replies; 19+ messages in thread From: Pascal J. Bourguignon @ 2013-03-23 11:31 UTC (permalink / raw) To: help-gnu-emacs Thorsten Jolitz <tjolitz@gmail.com> writes: > "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)) This is not an a-list, this is a dotted-list. Note that a-list entries can be proper-list, if the value is a proper-list, or circular lists, if the value is a circular list: (defvar *a* '((proper-list . (un one uno)) (circular-list . (deux . #1=(two dos . #1#))) (dotted-list . (un . deux)) (single-cell-dotted-list . single))) *a* --> ((proper-list un one uno) (circular-list deux . #1=(two dos . #1#)) (dotted-list un . deux) (single-cell-dotted-list . single)) (cdr (assoc 'proper-list *a*)) --> (un one uno) (cdr (assoc 'circular-list *a*)) --> (deux . #1=(two dos . #1#)) (cdr (assoc 'dotted-list *a*)) --> (un . deux) (cdr (assoc 'single-cell-dotted-list *a*)) --> single -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}. ^ permalink raw reply [flat|nested] 19+ messages in thread
* 'length' function for lists and cons cells? @ 2013-03-21 18:21 Thorsten Jolitz 2013-03-21 20:02 ` Stephen Berman ` (2 more replies) 0 siblings, 3 replies; 19+ messages in thread From: Thorsten Jolitz @ 2013-03-21 18:21 UTC (permalink / raw) To: help-gnu-emacs 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")) `-------------------------------------------------------------- -- cheers, Thorsten ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: 'length' function for lists and cons cells? 2013-03-21 18:21 Thorsten Jolitz @ 2013-03-21 20:02 ` Stephen Berman 2013-03-21 21:58 ` Thorsten Jolitz [not found] ` <mailman.22591.1363903134.855.help-gnu-emacs@gnu.org> 2013-03-21 20:10 ` Drew Adams [not found] ` <mailman.22588.1363896620.855.help-gnu-emacs@gnu.org> 2 siblings, 2 replies; 19+ messages in thread From: Stephen Berman @ 2013-03-21 20:02 UTC (permalink / raw) To: help-gnu-emacs On Thu, 21 Mar 2013 19:21:29 +0100 Thorsten Jolitz <tjolitz@gmail.com> wrote: > 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: Since a cons cells always contains exactly two members (its car and its cdr), why do you want to call a function to find its length? Steve Berman ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: 'length' function for lists and cons cells? 2013-03-21 20:02 ` Stephen Berman @ 2013-03-21 21:58 ` Thorsten Jolitz 2013-03-21 22:05 ` Drew Adams [not found] ` <mailman.22591.1363903134.855.help-gnu-emacs@gnu.org> 1 sibling, 1 reply; 19+ messages in thread From: Thorsten Jolitz @ 2013-03-21 21:58 UTC (permalink / raw) To: help-gnu-emacs Stephen Berman <stephen.berman@gmx.net> writes: >> 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: > > Since a cons cells always contains exactly two members (its car and its > cdr), why do you want to call a function to find its length? actually I'm looking for a way to distinguish between cons cells and regular lists to avoid the errors when mapping an alist that contains both with functions that work only on one of them, using 'length' was just a hack. I would have thought that 'consp' might be the right function, but: ,----------------------- | (consp '("a" "b" "c")) | t | | (consp '("a" . "c")) | t `----------------------- -- cheers, Thorsten ^ permalink raw reply [flat|nested] 19+ messages in thread
* RE: 'length' function for lists and cons cells? 2013-03-21 21:58 ` Thorsten Jolitz @ 2013-03-21 22:05 ` Drew Adams 0 siblings, 0 replies; 19+ messages in thread From: Drew Adams @ 2013-03-21 22:05 UTC (permalink / raw) To: 'Thorsten Jolitz', help-gnu-emacs > actually I'm looking for a way to distinguish between cons cells and > regular lists to avoid the errors when mapping an alist that contains > both with functions that work only on one of them, using 'length' was > just a hack. You distinguish them by the way they are distinguished ;-), i.e., by whether the cdr of the last cons cell is non-nil. A true list is either nil or a cons that has a true list as its cdr. This implies that it has nil as the cdr of its last cons. See my previous message. ^ permalink raw reply [flat|nested] 19+ messages in thread
[parent not found: <mailman.22591.1363903134.855.help-gnu-emacs@gnu.org>]
* Re: 'length' function for lists and cons cells? [not found] ` <mailman.22591.1363903134.855.help-gnu-emacs@gnu.org> @ 2013-03-23 11:24 ` Pascal J. Bourguignon 0 siblings, 0 replies; 19+ messages in thread From: Pascal J. Bourguignon @ 2013-03-23 11:24 UTC (permalink / raw) To: help-gnu-emacs Thorsten Jolitz <tjolitz@gmail.com> writes: > Stephen Berman <stephen.berman@gmx.net> writes: > >>> 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: >> >> Since a cons cells always contains exactly two members (its car and its >> cdr), why do you want to call a function to find its length? > > actually I'm looking for a way to distinguish between cons cells and > regular lists to avoid the errors when mapping an alist that contains > both with functions that work only on one of them, using 'length' was > just a hack. A-lists are proper lists, so I don't see what you're afraid of here. But again, have a look at: https://gitorious.org/com-informatimago/com-informatimago/blobs/master/common-lisp/cesarum/list.lisp#line96 > I would have thought that 'consp' might be the right function, but: > > ,----------------------- > | (consp '("a" "b" "c")) > | t > | > | (consp '("a" . "c")) > | t > `----------------------- It can be used. -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}. ^ permalink raw reply [flat|nested] 19+ messages in thread
* RE: 'length' function for lists and cons cells? 2013-03-21 18:21 Thorsten Jolitz 2013-03-21 20:02 ` Stephen Berman @ 2013-03-21 20:10 ` Drew Adams 2013-03-21 22:09 ` Thorsten Jolitz [not found] ` <mailman.22588.1363896620.855.help-gnu-emacs@gnu.org> 2 siblings, 1 reply; 19+ messages in thread From: Drew Adams @ 2013-03-21 20:10 UTC (permalink / raw) To: 'Thorsten Jolitz', help-gnu-emacs > 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)) ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: 'length' function for lists and cons cells? 2013-03-21 20:10 ` Drew Adams @ 2013-03-21 22:09 ` Thorsten Jolitz 0 siblings, 0 replies; 19+ messages in thread From: Thorsten Jolitz @ 2013-03-21 22:09 UTC (permalink / raw) To: help-gnu-emacs "Drew Adams" <drew.adams@oracle.com> writes: >> 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. I probably wasn't entirely clear to me either .. > 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)))))) > But with this function you answered the question I really wanted to ask: how to distinguish between a true list and a cons cell - by the use of 'atom' on the last cdr. Maybe pretty basic, but I haven't done it before. > (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)) Thanks, nice solution - very useful. -- cheers, Thorsten ^ permalink raw reply [flat|nested] 19+ messages in thread
[parent not found: <mailman.22588.1363896620.855.help-gnu-emacs@gnu.org>]
* Re: 'length' function for lists and cons cells? [not found] ` <mailman.22588.1363896620.855.help-gnu-emacs@gnu.org> @ 2013-03-25 15:24 ` duthen.cnv 2013-03-25 20:02 ` Pascal J. Bourguignon 0 siblings, 1 reply; 19+ messages in thread From: duthen.cnv @ 2013-03-25 15:24 UTC (permalink / raw) To: gnu.emacs.help; +Cc: help-gnu-emacs, Thorsten Jolitz Le jeudi 21 mars 2013 21:10:10 UTC+1, Drew Adams a écrit : > > 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)) 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!) could consider that (a b . c) is "a little bit longer" than (a b) 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 ! :) ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: 'length' function for lists and cons cells? 2013-03-25 15:24 ` duthen.cnv @ 2013-03-25 20:02 ` Pascal J. Bourguignon 0 siblings, 0 replies; 19+ messages in thread From: Pascal J. Bourguignon @ 2013-03-25 20:02 UTC (permalink / raw) To: help-gnu-emacs duthen.cnv@gmail.com writes: > 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!) > could consider that (a b . c) is "a little bit longer" than (a b) > 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 ! :) Nice. And since circular lists have a stem length and a loop length, with the loop length >0, we can represent them as complex numbers, perhaps something like: (* (+ stem-length loop-length) (expt (sqrt -1) (/ stem-length loop-length))) -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}. ^ permalink raw reply [flat|nested] 19+ messages in thread
end of thread, other threads:[~2013-03-25 20:02 UTC | newest] Thread overview: 19+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- [not found] <mailman.22586.1363890109.855.help-gnu-emacs@gnu.org> 2013-03-21 20:49 ` 'length' function for lists and cons cells? Pascal J. Bourguignon 2013-03-22 2:38 ` Thorsten Jolitz 2013-03-22 7:27 ` Mark Skilbeck 2013-03-22 9:57 ` Thorsten Jolitz [not found] ` <mailman.22632.1363946271.855.help-gnu-emacs@gnu.org> 2013-03-23 12:19 ` Pascal J. Bourguignon 2013-03-23 15:35 ` Drew Adams [not found] ` <mailman.22706.1364052941.855.help-gnu-emacs@gnu.org> 2013-03-23 15:52 ` Pascal J. Bourguignon 2013-03-24 5:20 ` Drew Adams [not found] ` <mailman.22618.1363919920.855.help-gnu-emacs@gnu.org> 2013-03-22 7:45 ` Barry Margolin 2013-03-23 11:31 ` Pascal J. Bourguignon 2013-03-21 18:21 Thorsten Jolitz 2013-03-21 20:02 ` Stephen Berman 2013-03-21 21:58 ` Thorsten Jolitz 2013-03-21 22:05 ` Drew Adams [not found] ` <mailman.22591.1363903134.855.help-gnu-emacs@gnu.org> 2013-03-23 11:24 ` Pascal J. Bourguignon 2013-03-21 20:10 ` Drew Adams 2013-03-21 22:09 ` Thorsten Jolitz [not found] ` <mailman.22588.1363896620.855.help-gnu-emacs@gnu.org> 2013-03-25 15:24 ` duthen.cnv 2013-03-25 20:02 ` Pascal J. Bourguignon
Code repositories for project(s) associated with this external index https://git.savannah.gnu.org/cgit/emacs.git https://git.savannah.gnu.org/cgit/emacs/org-mode.git This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.