unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* lists, plain conses and length
@ 2020-09-04  2:23 T.V Raman
  2020-09-04  2:26 ` Stefan Monnier
  2020-09-04  2:29 ` Lars Ingebrigtsen
  0 siblings, 2 replies; 5+ messages in thread
From: T.V Raman @ 2020-09-04  2:23 UTC (permalink / raw)
  To: emacs-devel


Ran into this while trying to distinguish a cons from a list:

Try this:

(setq a (cons 1 2)) => (1 . 2)
(consp a) => t
(listp a) => t
(length a)=> Debugger entered--Lisp error: (wrong-type-argument listp 2)

Q: how does one distinguish a cons from a list.
The error  message for length may need fixing since it says the
argument passed to it is not a list (it's correct) but then listp is wrong?
-- 
♉ Id: kg:/m/0285kf1  🦮

-- 
♉ Id: kg:/m/0285kf1  🦮

-- 
♉ Id: kg:/m/0285kf1  🦮



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: lists, plain conses and length
  2020-09-04  2:23 lists, plain conses and length T.V Raman
@ 2020-09-04  2:26 ` Stefan Monnier
  2020-09-04 14:19   ` T.V Raman
  2020-09-04  2:29 ` Lars Ingebrigtsen
  1 sibling, 1 reply; 5+ messages in thread
From: Stefan Monnier @ 2020-09-04  2:26 UTC (permalink / raw)
  To: T.V Raman; +Cc: emacs-devel

> Q: how does one distinguish a cons from a list.

A: check `null`

(tongue in cheek)


        Stefan


PS: Hint: `(listp X)` is the same as `(or (consp X) (null X))`




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: lists, plain conses and length
  2020-09-04  2:23 lists, plain conses and length T.V Raman
  2020-09-04  2:26 ` Stefan Monnier
@ 2020-09-04  2:29 ` Lars Ingebrigtsen
  2020-09-04  8:53   ` tomas
  1 sibling, 1 reply; 5+ messages in thread
From: Lars Ingebrigtsen @ 2020-09-04  2:29 UTC (permalink / raw)
  To: T.V Raman; +Cc: emacs-devel

"T.V Raman" <raman@google.com> writes:

> (length a)=> Debugger entered--Lisp error: (wrong-type-argument listp 2)
>
> Q: how does one distinguish a cons from a list.
> The error  message for length may need fixing since it says the
> argument passed to it is not a list (it's correct) but then listp is wrong?

`length' (and many other functions) require what's called a "proper
list", which simply means that the cdr of the last cons cell is nil.

(length '(1 2 . 3))
-> (wrong-type-argument listp 3)

The predicate to use here is `proper-list-p'.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: lists, plain conses and length
  2020-09-04  2:29 ` Lars Ingebrigtsen
@ 2020-09-04  8:53   ` tomas
  0 siblings, 0 replies; 5+ messages in thread
From: tomas @ 2020-09-04  8:53 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 784 bytes --]

On Fri, Sep 04, 2020 at 04:29:47AM +0200, Lars Ingebrigtsen wrote:
> "T.V Raman" <raman@google.com> writes:
> 
> > (length a)=> Debugger entered--Lisp error: (wrong-type-argument listp 2)
> >
> > Q: how does one distinguish a cons from a list.
> > The error  message for length may need fixing since it says the
> > argument passed to it is not a list (it's correct) but then listp is wrong?
> 
> `length' (and many other functions) require what's called a "proper
> list", which simply means that the cdr of the last cons cell is nil.
> 
> (length '(1 2 . 3))
> -> (wrong-type-argument listp 3)
> 
> The predicate to use here is `proper-list-p'.

Note that `proper-list-p' has to run down the whole list to make
sure. For short lists, that's fine.

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: lists, plain conses and length
  2020-09-04  2:26 ` Stefan Monnier
@ 2020-09-04 14:19   ` T.V Raman
  0 siblings, 0 replies; 5+ messages in thread
From: T.V Raman @ 2020-09-04 14:19 UTC (permalink / raw)
  To: monnier; +Cc: raman, emacs-devel

In the spirit of the US election campaign, if it isn't shown to be
true, it must be false -- if it's shown to be false, it  must be fake ...Stefan Monnier writes:
 > > Q: how does one distinguish a cons from a list.
 > 
 > A: check `null`
 > 
 > (tongue in cheek)
 > 
 > 
 >         Stefan
 > 
 > 
 > PS: Hint: `(listp X)` is the same as `(or (consp X) (null X))`

-- 
♉Id: kg:/m/0285kf1  🦮♉

--
♉Id: kg:/m/0285kf1  🦮♉



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-09-04 14:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-09-04  2:23 lists, plain conses and length T.V Raman
2020-09-04  2:26 ` Stefan Monnier
2020-09-04 14:19   ` T.V Raman
2020-09-04  2:29 ` Lars Ingebrigtsen
2020-09-04  8:53   ` tomas

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).