Re: How do I distinguish a list from cons pair?
On Tue, Feb 11, 2025 at 11:38 PM Alexander Burger wrote: > I would say it is a cell with a non-NIL and non-atomic CDR, then (1 . 2) > is not a list but > >(1 . NIL) # (1) > > or > >(1 . (2 . NIL)) # (1 2) > > is. > > 👍 BTW, 'fin' can be used to distinguish them: > >: (fin (1 2)) >-> NIL >: (fin (1 2 . 3)) >-> 3 > > 'fin' may be the function I am looking for. I will have to play with it a bit and probably rethink my data structure. I was trying to 'rotate' a tree structure and for the purpose of 'set' I needed to distinguish between (1 . 2) (1 2) (1 . (2 3 4)) (1 (2 3 4)) In the case of the pair, I can't use 'set' to update the cdr and would have to use 'con' and reference the appropriate node. : (lst? (cdr Pair)) > I tried that expression, but the way I was thinking about lists, and trying to use them, didn't give me the results I expected. : (lst? (cdr (1 . 2))) -> NIL : (lst? (cdr (1 2))) -> T : (lst? (cdr (1 . (2 3 4 -> T : (lst? (cdr (1 (2 3 4 -> T /Lindsay
Re: How do I distinguish a list from cons pair?
On Wed, Feb 12, 2025 at 08:31:03AM +0200, GB wrote: > Lists are kind of a virtual type, it's just a bunch of cons cells in a > sequencial pattern. Exactly. > I think you'd need to write your own function that walks the list to see if > its proper or improper (ends with non-nil). Right. 'fin' does this though. ☺/ A!ex -- UNSUBSCRIBE: mailto:[email protected]?subject=Unsubscribe
Re: How do I distinguish a list from cons pair?
On Tue, Feb 11, 2025 at 10:16:14PM -0800, Lindsay Lawrence wrote: > Is there a way to distinguish a list from a cons pair? > > The 'pair' function does not seem to do what I want. It depends on how you define a list. I would say it is a cell with a non-NIL and non-atomic CDR, then (1 . 2) is not a list but (1 . NIL) # (1) or (1 . (2 . NIL)) # (1 2) is. I would also call (1 . (2 . 3)) # (1 2 . 3) a list. BTW, 'fin' can be used to distinguish them: : (fin (1 2)) -> NIL : (fin (1 2 . 3)) -> 3 > I would like to be able to distinguish the cons pair structure from 'list' So perhaps : (lst? (cdr Pair)) ☺/ A!ex -- UNSUBSCRIBE: mailto:[email protected]?subject=Unsubscribe
Re: How do I distinguish a list from cons pair?
Hey, I'm curious myself honestly. Lists are kind of a virtual type, it's just a bunch of cons cells in a sequencial pattern. I think you'd need to write your own function that walks the list to see if its proper or improper (ends with non-nil). Best regards, Geri On Wed, Feb 12, 2025, 08:28 Lindsay Lawrence wrote: > Is there a way to distinguish a list from a cons pair? > > The 'pair' function does not seem to do what I want. > > : (pair (cons 1 2)) > -> (1 . 2) > : (pair (list 1 2)) > -> (1 2) > : (pair (list 1 (2 3))) > -> (1 (2 3)) > : (pair (cons 1 (2 3))) > -> (1 2 3) > > I would like to be able to distinguish the cons pair structure from 'list' > > /Lindsay > > : (view (cons 1 2)) > +-- 1 > | > 2 > > : (view (list 1 2)) > +-- 1 > | > +-- 2 > > : (view (cons 1 (2 3))) > +-- 1 > | > +-- 2 > | > +-- 3 > > : (view (list 1 (2 3))) > +-- 1 > | > +---+-- 2 > | > +-- 3 > >>
