[HN Gopher] Why does Lisp use cons cells? (1998)
       ___________________________________________________________________
        
       Why does Lisp use cons cells? (1998)
        
       Author : sph
       Score  : 65 points
       Date   : 2024-10-02 07:49 UTC (2 days ago)
        
 (HTM) web link (www.xach.com)
 (TXT) w3m dump (www.xach.com)
        
       | kragen wrote:
       | Well, shit. Educational, but filled with bile. This post is an
       | example of how to go wrong and make yourself and everyone around
       | you unhappy, while still being right.
        
         | LAC-Tech wrote:
         | I'd rather an Erik Naggum than milquetoast passive
         | aggressiveness. If you're going to be a dick, at least be up
         | front with it.
        
           | biorach wrote:
           | Or, just don't be a dick, full stop.
        
           | NikkiA wrote:
           | The thing that always amused me about Erik was that he was
           | often right, but dismissed because of his tone or reputation.
        
         | agumonkey wrote:
         | He made it a kind of game or culture. Also iiuc he's answering
         | to a certain person who was known to be regularly walking on
         | the line of trolling. That might have added more fuel to the
         | fire.
        
         | stackghost wrote:
         | He's responding to Xah Lee, well known to be one of the most
         | abrasive and unpleasant people in the lisp community. The
         | hostility might not be desired but it was certainly earned.
        
           | QuesnayJr wrote:
           | It was Erik Naggum replying to Xah Lee, so you had two of the
           | most abrasive and unpleasant people in the lisp community
           | interacting.
        
           | dokyun wrote:
           | It doesn't look like Xah was really trolling here, most of
           | his responses are pretty polite. And he seems to indicate at
           | this point in time he wasn't really familiar with lisp, so
           | I'd venture to guess he wasn't nearly as prominent as he
           | later became.
        
             | stackghost wrote:
             | You may be correct; I generally enjoy not thinking about
             | Xah Lee so I might not have the correct context for TFA's
             | point in time
        
             | p_l wrote:
             | Xah has talent for speaking out of his ass in polite and
             | knowledgeable sounding way
        
               | exe34 wrote:
               | the original LLM
        
               | Jach wrote:
               | Most of the stuff I've seen has not been so polite, he's
               | such a notorious hater. Still, it can be entertaining in
               | a way, in small doses (I imagine I'd feel a bit different
               | if I was forced to share a newsgroup commons for years),
               | and such a critic can provide a good perspective once in
               | a while. Even though he's still around I think of him as
               | a character as much as Naggum was, even if they were very
               | different in important ways. The world is richer from
               | having more characters.
               | 
               | The other day I ran into a "gem" from Xah that just made
               | me laugh at its expression. Willing to tank the
               | karma/flag to reproduce:                   1 I fucking
               | hate this motherfucking tech geeking hacker fucks who
               | leave dead links on the web and don't fucking care. What
               | the fuck?         Aren't these elite, elegant, coding
               | experts who always take perfection         in algorithm,
               | code, style, language, etc?                  I don't
               | understand, how could they, have their home page dead?
               | 
               | I laughed again when some followup links to his site
               | 404'd. It's not at all a fair attack, but man, sometimes
               | I _really_ hate dead links too...
        
               | dokyun wrote:
               | I think he's hilarious, and at a time where a lot of
               | programming pundits only pretend to be subversive and
               | shameless, I welcome Xah's unflitered presentation of his
               | opinions. He often makes a lot of good points, and I tend
               | to find his stances on things thought-provoking even if I
               | disagree with him.
        
               | NikkiA wrote:
               | He's my least favourite emacs user
        
           | lispm wrote:
           | Xah Lee wasn't a part of the Lisp community. He was a Usenet
           | troll, active in several programming related newsgroups, not
           | just comp.lang.lisp. He had never written a line of Lisp (or
           | anything more challenging) and never contributed anything,
           | during his Usenet times. His effect on these newsgroups was
           | extremely negative. Discussing with him was useless. Erik
           | Naggum tried and failed (as others did). Erik was a part of
           | the Lisp community, but unfortunately also with negative
           | behavior, especially on comp.lang.lisp. He could write
           | long&useful explanations, but discussions with him were often
           | not pleasant.
        
         | anonzzzies wrote:
         | It is possible for your brain to parse information from people
         | you don't have a personal connection with as what the message
         | is and skip the how. Not sure why people get lit up by the how;
         | who cares about that from people you don't give a toss about?
         | These days you have AI for it; Claude can give you a lovely
         | summary of the facts without the how. My brain does that
         | automatically since I was in high school. Comments from anyone
         | but close friends or close relatives cannot make me 'unhappy',
         | no matter what they are.
        
           | johnisgood wrote:
           | This is getting out of hand, "feelings over facts" kinda
           | thing these days, people are unable to deal with petty
           | "insults", etc.
        
         | bsder wrote:
         | Erik Naggum (RIP) was particularly noted for having lots of
         | bile if you got on the wrong side of him.
         | 
         | Xah Lee is noted for being especially abrasive and pretty much
         | gets on the wrong side of everybody.
         | 
         | Putting the two together was always a spectacle worth a bucket
         | or two of popcorn.
         | 
         | And, the real answer to "Why cons cells?" is the same reason as
         | "What is up with the names car and cdr?". The only two data
         | structures which are cheap enough (in both RAM and CPU) for the
         | machines of the time (IBM 704 from 1954!) to deal with are
         | arrays (via index registers) and pairs (via register sections
         | which gives us car/cdr).
        
         | dreamcompiler wrote:
         | That was Naggum's trademark: Extremely good explanations
         | coupled with zero tolerance for fools. As long as you asked him
         | a question in good faith, he treated you with courtesy. But I
         | confess I relished the entertainment of reading his
         | deconstructions of bad-faith actors.
         | 
         | Naggum died much too young. I hope he has found peace.
         | 
         | https://en.m.wikipedia.org/wiki/Erik_Naggum
        
           | moron4hire wrote:
           | I can't stand this style of writing. I especially dislike it
           | when I catch myself doing it.
           | 
           | It's not necessarily because of the negativity. It's mostly
           | just because people who write like this take _forever_ to get
           | to their point.
        
             | kazinator wrote:
             | A certain Tim R., a present day comp.lang.c poster, takes
             | multiple posts spanning weeks to get to the point.
        
               | moron4hire wrote:
               | And these people always say they're "just being direct".
               | 
               | It's a good litmus test for me in my own writing. Am I
               | actually being direct or just indulging in anger? What
               | happens if I just get to the point and say what I want? I
               | have found I get more of what I want that way.
        
           | e40 wrote:
           | It's been a while since I've seen those photos on the
           | wikipedia page, which I took. He was very photogenic. He came
           | to Berkeley for a Lisp conference. Brings back some really
           | good memories. Thanks.
        
             | dreamcompiler wrote:
             | I met him (and McCarthy too) at that conference. Can't
             | believe it's been 25 years.
        
           | eadmund wrote:
           | I think the issue is distinguishing between bad faith and
           | simple ignorance.
           | 
           | You catch more flies with honey than vinegar!
        
             | kazinator wrote:
             | Fruit vinegar (e.g. apple cider) is the tool of choice for
             | trapping fruit flies.
        
           | kragen wrote:
           | Naggum destroyed the Lisp community by driving away everyone
           | who didn't relish it. (Like you, Xah Lee did relish it, even
           | when the bad-faith actor being "deconstructed" was a
           | character he was playing--perhaps especially then!) He also
           | had, as you can see in this post, zero tolerance for people
           | preferring different tradeoffs from his own (often because
           | they held different values), which fragmented it.
        
             | kazinator wrote:
             | comp.lang.lisp is not the same thing as "the Lisp
             | community".
             | 
             | Even in the heyday, the Usenet news groups represented a
             | minority. For any language, OS or tech stack, there were
             | many more programmers off Usenet than on.
             | 
             | A programming language newsgroup represents the
             | intersection between interest in that language and interest
             | in Usenet.
        
             | lispm wrote:
             | German has this great word for your comment:
             | "unterkomplex". The English "simplistic" might come near.
             | comp.lang.lisp was not "the Lisp community", Naggum did not
             | single handedly destroy it, you ignore the other
             | participants, the general Usenet dynamics and its overall
             | fate...
        
               | kragen wrote:
               | Usenet was important for the development of many other
               | language communities, and Lisp was denied that
               | opportunity. Naggum didn't act alone, certainly, and bad
               | blood existed in the Lisp community since before Usenet
               | (Stallman is a difficult person, and we can all be
               | grateful he never actually bombed the Symbolics offices),
               | but he _was_ the pivotal figure in how things played out
               | in the late 01990s. There wasn 't a Naggum-free
               | alternative general Lisp venue where new users (or
               | Lispers who merely disagreed with Naggum, such as
               | Schemers) could post without fear of public humiliation,
               | so mostly they didn't. It's a rare undergraduate who
               | doesn't count public humiliation among their worst fears,
               | and for people like Steele, Weinreb, Gabriel, Friedman,
               | and Stallman, participation on Usenet was all downside
               | with no upside.
               | 
               | Lisp started to recover immediately when Naggum died. Or,
               | more accurately, when he got too sick to post.
               | 
               | I could make the same criticism of HN, but to a much
               | smaller degree.
               | 
               | Kaz is of course correct that in Lisp, as in any
               | language, there's a silent majority of programmers who
               | don't post in public forums like Usenet. But they depend
               | on the efforts of those who do for things like FAQs and
               | free software.
        
               | lispm wrote:
               | You are, sadly, digging yourself deeper into a hole. I'm
               | not interested to follow you there.
        
               | kragen wrote:
               | None of this has to do with me; I can't have posted to
               | comp.lang.lisp more than a few times, and I don't recall
               | ever interacting with Naggum.
               | 
               | Most of what I've said is self-evidently correct, and
               | almost all of the rest is relatively easy to verify by
               | reading the archives of old mailing lists and Usenet.
               | 
               | The only exception is my insinuation that Stallman was
               | tempted to bomb the Symbolics offices, which I don't have
               | a reliable source for; maybe you could find out by asking
               | Stallman, or Stallman's former friends and former
               | coworkers.
        
               | lispm wrote:
               | "self-evidently correct" is not a good word if you want
               | to discuss things with people who were actually active in
               | the wider Lisp communities, including comp.lang.lisp,
               | other newsgroups, various mailing lists, Lisp
               | conferences, etc.
        
               | kragen wrote:
               | I'm interested to hear your alternative perspective, but
               | as long as I lack it, I'm going to have to agree with
               | dang: https://news.ycombinator.com/item?id=26855767
               | 
               | My objective here is not to attack you for your partisan
               | support of Naggum; rather, I want to understand what
               | alternative perspective you're coming from in which my
               | observations perhaps _aren 't_ self-evidently correct--
               | though I note that you've carefully avoided actually
               | _saying_ they aren 't, leaving open the interpretation
               | that it's just that you wish I'd phrase it differently.
        
               | lispm wrote:
               | > My objective here is not to attack you for your
               | partisan support of Naggum
               | 
               | Look, I'm not supporting Naggum.
               | 
               | From the actual Lisp users I've met in real life, few
               | were reading comp.lang.lisp, almost none was posting
               | there. Very few would even know who the people there
               | were.
               | 
               | The actual people in the Lisp community I admire are many
               | - I could list 100 - they had a much greater impact on
               | what was happening. Most of them never used
               | comp.lang.lisp . Among women the participation rate was
               | near zero!
               | 
               | Most of the interesting communication was in specific
               | communities over mailing lists. I fondly remember SLUG
               | (the Symbolics Lisp Users Group), the MCL mailing list,
               | and several others.
               | 
               | comp.lang.lisp had the most negative impact by trolls,
               | like the one Naggum was answering, which caused endless
               | bullshit discussions and which had very destructive
               | behavior. We never found a way to deal with that. What it
               | killed in the end was that the time for Usenet was over,
               | Google groups, spam, it was mostly irrelevant and a much
               | more toxic Internet took over. See 4chan, various Reddit
               | groups (though the Lisp subreddits at Reddit do better)
               | and X for much worse communication systems. Plus Lisp was
               | in a general demise in the 90s. Not the Usenet caused the
               | demise of Lisp, but the demise of Lisp was causing a
               | general lack of interest. Cause and effect were different
               | of what you claim.
               | 
               | For you the contact to Lisp could be mostly net related
               | (?). When I was learning at the university roughly up to
               | 20 professors were in some way or another involved in
               | Lisp projects. There were people I've seen in _real life_
               | , often daily. There were many people with deep Lisp
               | knowledge (and this was not even the US). You can bet
               | that none of them used or cared about comp.lang.lisp.
               | Same for the people in the projects and the students.
               | 
               | What you think was "the Lisp community", was just a
               | popular online community of mostly random people, with
               | less impact on what actually happened in the Lisp
               | communities, then you believe.
        
         | pavlov wrote:
         | Brings back unhappy memories of the conversation style in
         | Usenet. I didn't dare to use newsgroups after I asked some
         | wrong thing as a 15-year-old in some computing-related group
         | and got dumped on by angry adult men.
         | 
         | I get it that they were frustrated by Eternal September and
         | whatever. But thinking back to those times makes me appreciate
         | the moderation culture that dang has created here, as well as
         | the non-linear structure that moves less popular comments out
         | of the way of attention.
        
           | dang wrote:
           | Similar memories have been an influence on HN's moderation
           | style. I've mentioned that a few times over the years:
           | 
           | https://news.ycombinator.com/item?id=34328146 (Jan 2023)
           | 
           | https://news.ycombinator.com/item?id=32725068 (Sept 2022)
           | 
           | https://news.ycombinator.com/item?id=28832073 (Oct 2021)
           | 
           | https://news.ycombinator.com/item?id=26855767 (April 2021)
           | 
           | https://news.ycombinator.com/item?id=19773300 (April 2019)
           | 
           | https://news.ycombinator.com/item?id=11839842 (June 2016)
           | 
           | https://news.ycombinator.com/item?id=10979050 (Jan 2016)
           | 
           | https://news.ycombinator.com/item?id=9813527 (July 2015)
           | 
           | - and more generally https://hn.algolia.com/?dateRange=all&pa
           | ge=0&prefix=true&que...
           | 
           | Occasionally I run into posts from the heyday of
           | comp.lang.lisp and it's still astonishing how high-quality
           | the discussion was, before it wasn't.
        
             | kragen wrote:
             | I appreciate your work; I can see how difficult it must be.
        
       | Jtsummers wrote:
       | https://groups.google.com/g/comp.lang.lisp/c/T_Tozq2P53I/m/N...
       | 
       | The full conversation.
        
       | agency wrote:
       | They're also the original and simplest persistent data structures
       | https://en.wikipedia.org/wiki/Persistent_data_structure
        
         | guerrilla wrote:
         | What criteria do you use to judge those two properties?
        
       | agambrahma wrote:
       | Ah a Naggum re-discovery.
       | 
       | See a yearly index here: https://www.xach.com/naggum/articles/
       | 
       | I went through the whole thing a few years ago, there are some
       | gems hidden in there.
        
       | kagevf wrote:
       | This doesn't come across as "caustic" as when I previously read
       | it. And the "bile" isn't directed so much at Lee as it is at
       | something he wrote; "All in all, the pairs notion is redundant."
       | 
       | Naggum genuinely seems to be hoping that his long and thorough
       | explanation will convince Lee of his point of view, and not as a
       | put down.
       | 
       | > I hope you understand and appreciate what I have written above
       | so the following does not apply to you anymore.
       | 
       | The "following" where he goes off on what Lee wrote, but not on
       | Lee himself.
       | 
       | It might be worth it for me to re-read Naggum's posts, now that I
       | have a better understanding of where he was coming from.
        
         | p_l wrote:
         | Unfortunately, from my experience, Xah Lee is someone who will
         | ignore information from others if it requires to correct what
         | he wrote/spoke, especially when he claims something
         | authoritatively when in reality he has no in-depth, if any,
         | information on the subject
        
           | kagevf wrote:
           | He does have a tendency to write off people as "fanatics".
        
         | Joker_vD wrote:
         | Not caustic? "Short-sighted users", "stupid language designers
         | who believe too strongly in strong typing", "certainly can't be
         | bothered to invent a useful concept for their short-sighted
         | programmers", "dumb people who implement something the simple
         | way, so it becomes complex and generally useless to everybody
         | else", "unlike today's braindamaged CPU's", "just look at what
         | C programmers do when they need lists! _shudder_ ".
         | 
         | Sure, you may believe that LISP is the greatest invention since
         | the sliced bread, and everything went downhill after that but
         | even if you are correct -- that's still just, like, your
         | opinion, man.
        
           | kagevf wrote:
           | I wrote "not *as* caustic" ... in particular, he wasn't being
           | caustic toward Lee, but rather to the idea of cons pairs
           | being redundant.
           | 
           | It's enough of distinction to make me think I should re-
           | examine what Naggum had to say - in this and in other posts -
           | and not get caught up in any pre-conceived notions I might
           | have had about him.
        
           | abecedarius wrote:
           | It was pretty mild for Naggum. Like the GP I was expecting a
           | higher density of that sort of thing, but maybe my memory is
           | too slanted by his worst flames.
        
           | nescioquid wrote:
           | Consider that you may be eavesdropping on the sort of self-
           | talk the author engages in. The clear reasoning mixed with
           | vituperation makes me suspect he probably beats himself up.
           | There is a lot of worry over being mistaken and stupid.
           | 
           | I'm unfamiliar with the author, so I could be way off base.
        
       | mst wrote:
       | Anybody interested in efficient list (and other data structures,
       | but mostly lists) implementation will probably find the VList
       | approach interesting (relevant paper archived at
       | https://trout.me.uk/lisp/vlist.pdf)
        
         | deepnet wrote:
         | [post] tl;dr
         | 
         | A ( linked ) list is a collection of CONS ( Construct ) cells.
         | 
         | A CONS cells can extract, via the commands:
         | 
         | 'car' ( Contents of Address Register ) - the payload ( data
         | item in list );
         | 
         | 'cdr' ( Contents of Decrement Register ) - the link ( to the
         | next list item, e.g. CONS cell )
         | 
         | The names are for historical reasons ( using two halves of a
         | single register ) lost in the beards of yore.
         | 
         | The linked list appears simple, this belies the fact of its
         | exquisite perfection and the great thought that went into
         | inventing, perfecting and implementing it.
         | 
         | "[The 'list' command]... creates a series of conses and sets
         | the CAR pointers to point to each argument. which you have
         | provided. (LIST (LIST 1 2) (LIST 3 4)) => ((1 2) (3 4))
        
       | kerkeslager wrote:
       | Erik Naggum's writings were pretty influential on me as a young
       | programmer, and there's still some value in this piece, but
       | nowadays I find a lot of this insufferable. Yes, it's not
       | actually easy to implement lists, and there's semantic value to
       | Common Lisp's implementation. But...
       | 
       | 1. You can't tout the benefits of constant time appending to the
       | beginning of a list while ignoring the benefits of constant time
       | lookup and modification within the list. And before anyone starts
       | talking about "but most programs don't need random access because
       | you just traverse the list"--yes, and vectors do that better too
       | because of cache locality. Cache-aware implementations of Lisps
       | are still an order of magnitude slower than vectors for
       | traversals because dereferencing the cdr pointer is obligatory.
       | If you start talking about speed being a benefit of Lisp in
       | comparison to C (a comparison which Naggum introduces later
       | here), you've lost the plot.
       | 
       | 2. Nostalgia for an architecture that no longer existed even in
       | 1998 when this was written is poor reasoning for opaque names.
       | "car" and "cdr" _aren 't_ good names. It kind of doesn't matter
       | much: once you learn them it's fine, but it _is_ a slight barrier
       | to Lisp 's success, as it does present yet another small
       | challenge for beginning Lisp programmers.
       | 
       | 3. In general, the ragging on Scheme is rather pointless--I found
       | my way to Lisp _via_ Scheme. And a large part of why I ended up
       | back in Scheme land (well, Racket by that point) was that it
       | became clear to me that the Scheme community benefits from ideas
       | learned in Common Lisp, while Common Lisp often (not always)
       | rejects NIH ideas.
        
         | pfdietz wrote:
         | It would be trivial to replace car and cdr in your code with
         | any other symbol names you like. Even the Common Lisp standard
         | supplies alternatives (first, rest). If this was not done, it
         | was because people using lisp didn't see any great value in
         | doing it.
        
           | Majromax wrote:
           | Your point and the grandparent's point are not contradictory.
           | 'car' and 'cdr' can be opaque pieces of jargon that make the
           | language slightly but noticeably more difficult for new
           | learners, and they also can be _standard_ pieces of jargon
           | that current users would find no value in changing.
           | 
           | In that way, it's a bit like indentation in Python.
        
             | pfdietz wrote:
             | I think this illustrates that perceived shallow blemishes
             | that upset newbies have nothing to do with success or
             | failure of a programming language. Another example here is
             | "all those parentheses", and perhaps even lack of static
             | typing.
        
             | zahlman wrote:
             | I would say that it's more like the `def` keyword in
             | Python. People learn that it's short for "define" - but
             | that doesn't make much sense given that other kinds of
             | things can perfectly well be "defined". It also leads to
             | beginners expressing themselves weirdly - I've read things
             | like "so I have a defined function like so:" countless
             | times.
        
               | theonemind wrote:
               | What do you find strange about someone saying they
               | defined a function?
               | 
               | Just curious. I can't find anything strange about the
               | wording or conceptual understanding likely behind such a
               | statement.
        
         | rjsw wrote:
         | Lisp has had vectors as well as lists for a long time. If you
         | want a vector then use a vector.
        
           | Jach wrote:
           | Yup, though usual caveats on if the item stored is itself a
           | fat object requiring a pointer chase... SIMD exists as well,
           | SBCL has it available out of the box through sb-simd. SBCL
           | also tries to be a bit smart with its linked lists: when
           | first making a list, if you make it sequentially (make the
           | cons cells sequentially), it'll all be allocated
           | sequentially. And regardless of that, when the garbage
           | collector moves a list, its new location will be sequential.
           | 
           | The OCaml people at Jane Street noted that if you just focus
           | on cache lines, the overhead these days between list
           | traversal vs. vector traversal can be closer to a factor of
           | 2. Like 16 cycles vs. 30 cycles for a new cache line on an
           | old Skylake architecture, thanks to prefetching -- explicit
           | prefetching instructions are hard to get right, but CPUs have
           | also gotten better in the last 10 years at automatic
           | prefetching. (It might be even better on Apple's architecture
           | which I believe has more line fill buffers?) A miss is still
           | around 300 cycles though, and a cache line of 64 bytes very
           | typically represents more than one item (e.g. 8 longs).
        
         | AnimalMuppet wrote:
         | > And before anyone starts talking about "but most programs
         | don't need random access because you just traverse the list"--
         | yes, and vectors do that better too because of cache locality.
         | 
         | Valid, though at the time (even 1998!) caches were less
         | critical for performance than they are now, because the gap
         | between processing speed and main memory speed was smaller. In
         | fact, on the machines where Lisp originated, there was no
         | cache.
        
           | lispm wrote:
           | Lisp programs often needed large amounts of virtual memory.
           | Factor ten (VM size vs. RAM size) was not unheard. RAM was
           | basically a cache for that.
        
       | pfdietz wrote:
       | comp.lang.lisp was one of the meatier parts of Usenet I read.
        
       | e40 wrote:
       | The strange thing about Erik is that he was sweet, gentle and
       | helpful in person. I spent a fair amount of time with him, and
       | never once saw any of the behavior he was famous for on usenet.
       | 
       | He was highly intelligent but he had an odd childhood and was
       | somewhat isolated socially, in the real world. It's what
       | ultimately killed him. At least outside of tech. Near the end he
       | was almost a cult-like figure, at conferences especially. He
       | would accumulate a hoard of fans. I'm unsure if he liked that.
       | 
       | He really loved coming to the Bay Area.
       | 
       | I definitely miss him irl.
       | 
       | Sorry for the ramblings. I'm on mobile and editing is hard.
        
       | susam wrote:
       | The original discussion is here:
       | https://groups.google.com/g/comp.lang.lisp/c/T_Tozq2P53I/m/N...
       | 
       | I found it intriguing that the original post there claims that
       | (cons (list 1 2) (list 3 4))
       | 
       | intuitively seems to have the length 2 whereas the correct length
       | is 3. I find 3 more intuitive though. It could be because right
       | from my early days of learning various Lisps, I learnt about
       | lists in terms of cons cells.
       | 
       | There are many ways to explain this, such as by drawing diagrams
       | of the cons cells from first principles, printing out each cons
       | and list on a REPL and confirming the equivalences, etc. Here's
       | one that happens to be my take on it. I'll be (ab)using the
       | equals sign (=) to mean that the expression on the LHS and the
       | one on the RHS are equivalent.
       | 
       | First let us build a list of three items using cons cells:
       | (cons 4 nil) = (list 4)            (cons 3 (cons 4 nil)) = (list
       | 3 4)                [1]            (cons 'x (cons 3 (cons 4
       | nil))) = (list 'x 3 4)   [2]
       | 
       | Now let us build a list of two items using cons cells:
       | (cons 1 nil) = (list 1)            (cons 1 (cons 2 nil)) = (list
       | 1 2)                [3]
       | 
       | Now substituting LHS and RHS of [3] into 'x in LHS and RHS of
       | [2], respectively, we get                 (cons (cons 1 (cons 2
       | nil)) (cons 3 (cons 4 nil))) = (list (list 1 2) 3 4)
       | 
       | Simplifying the LHS above using [3] and [1], we get
       | (cons (list 1 2) (list 3 4)) = (list (list 1 2) 3 4)
       | 
       | Clearly, the length of RHS is 3.
       | 
       | From the REPL:                 CL-USER> (cons (list 1 2) (list 3
       | 4))       ((1 2) 3 4)       CL-USER> (list (list 1 2) 3 4)
       | ((1 2) 3 4)       CL-USER> (length (cons (list 1 2) (list 3 4)))
       | 3       CL-USER> (length (list (list 1 2) 3 4))       3       CL-
       | USER>
       | 
       | Also, on Emacs:                 ELISP> (cons (list 1 2) (list 3
       | 4))       ((1 2)        3 4)            ELISP> (list (list 1 2) 3
       | 4)       ((1 2)        3 4)            ELISP> (length (cons (list
       | 1 2) (list 3 4)))       3 (#o3, #x3, ?\C-c)       ELISP> (length
       | (list (list 1 2) 3 4))       3 (#o3, #x3, ?\C-c)       ELISP>
       | 
       | See also: https://gigamonkeys.com/book/they-called-it-lisp-for-a-
       | reaso...
        
         | kagevf wrote:
         | To me, the form is not intuitive, but the result is:
         | ((1 2) 3 4)
         | 
         | Someone else in that thread mentioned that CONS is basically
         | adding (1 2) to the front of the (3 4) list, and that helped me
         | understand it better.
        
         | abecedarius wrote:
         | That original post started with
         | 
         | > (list 1 2 3) == (cons 1 (cons 2 (cons 3 nil)))
         | 
         | and I'd have answered that cons could be a variable-arity
         | function such that
         | 
         | > (list 1 2 3) == (cons 1 2 3 nil)
         | 
         | and maybe that'd help this newbie's intuition. (Common Lisp
         | does have this variable-arity cons but by a different name
         | list*, for some reason.)
        
         | zahlman wrote:
         | > intuitively seems to have the length 2 whereas the correct
         | length is 3. I find 3 more intuitive though. It could be
         | because right from my early days of learning various Lisps, I
         | learnt about lists in terms of cons cells.
         | 
         | That's just how cons is defined. the first argument is a list
         | and the second is an arbitrary object, and the result has the
         | elements from the list plus the other object. It doesn't take a
         | bunch of algebraic manipulation to understand this definition.
         | It just takes a recognition that an "arbitrary object" could be
         | a list, as well as a recognition that the other definition you
         | imply ("make a new list with both arguments as elements") would
         | have to either be variadic (which in turn would obviate `list`)
         | or would only ever be able to make lists of exactly 2 elements.
         | (The point is that `cons` can serve as a simpler primitive, and
         | `list` can be implemented in terms of it.)
        
       ___________________________________________________________________
       (page generated 2024-10-04 23:02 UTC)