6 ms·
cons is a recursive data structure. it’s not discussed much here in the comments, but a lot of its useability comes from that fact - you can write elegant recur
by math-dev 4y ago
cons is a recursive data structure. it’s not discussed much here in the comments, but a lot of its useability comes from that fact - you can write elegant recursive algorithms with cons as your data structure
with vectors that doesn’t work UNLESS you add a bit of overhead (which most optimising programs may do since for many cases vectors can be more performant)
- User23 4y agoAnd some Lisp implementations optimize some cons lists into vectors. The technique is called CDR encoding[1]. It's my understanding that at least the latest generation of modern CPUs actually have optimized instructions for tagged 64 bit pointers too, so this can be implemented efficiently on current hardware just as it was on Lisp machines! Of note though is that mutability complicates things, as usual. [1] https://en.wikipedia.org/wiki/CDR_coding https://en.wikipedia.org/wiki/CDR_coding
- math-dev 4y agoThanks for sharing this link, very useful reading
- lispm 4y agoIt's just that since decades Lisp implementations don't do this, since most implementors don't see it providing much advantage (in speed or size) and it complicates the implementation.
- sillysaurusx 4y agoIncidentally, that link proves the point: > In the presence of mutable objects, CDR coding becomes more complex. If a reference is updated to point to another object, but currently has an object stored in that field, the object must be relocated, along with any other pointers to it. Not only are such moves typically expensive or impossible, but over time they cause fragmentation of the store. This problem is typically avoided by using CDR coding only on immutable data structures. This is exactly what I've been saying. Thank you for providing a formal reference to the idea. (I'm a bit confused how we wound up talking past each other, since my original proposal was identical to CDR coding on immutable cons cells.)