TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Data Structures in Clojure: Singly-Linked Lists

67 pointsby llambdaover 11 years ago

6 comments

aredingtonover 11 years ago
One of Clojure&#x27;s fundamental opinions, asserted by the language eagerly, is that vector quantities should be treated as values the way scalar quantities are in most languages. Specifically, this means that when you operate with a list and append to it, instead of modifying the value of the next (cdr) pointer in the last node, you create a new list and return the new list as the result of the append operation. These immutable collections afford certain classes of algorithm that destructively modified ones do not. Rich reviews this concept pretty well here: <a href="http://www.infoq.com/presentations/Value-Values" rel="nofollow">http:&#x2F;&#x2F;www.infoq.com&#x2F;presentations&#x2F;Value-Values</a><p>This story doesn&#x27;t spend much time discussing this particular opinion of Clojure&#x27;s or why you would choose to circumvent it by making a mutable linked list. Additionally by virtue of skipping the immutability discussion, it fails to discuss how append to the head of a list is crucially different from append to the tail when working with data structurally sharing underlying data.<p>Additionally it prefers to define an interface over a protocol (using definterface instead of defprotocol), and it uses camelCase naming instead of Clojure&#x27;s culturally embraced hyphen-cased naming.<p>In short this describes a way to build a data structure in Clojure, but is not a particularly good as an example to be followed of how you should build data structures in Clojure. johnwalker cited a couple more idiomatic examples, e.g. data.finger-trees: <a href="https://github.com/clojure/data.finger-tree/blob/master/src/main/clojure/clojure/data/finger_tree.clj" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;clojure&#x2F;data.finger-tree&#x2F;blob&#x2F;master&#x2F;src&#x2F;...</a>
stiffover 11 years ago
Those days I guess the phrase &quot;linked list&quot; shouldn&#x27;t really appear without the phrase &quot;locality of reference&quot; somewhere near. From what I understand, the only practical context in which classic linked lists could be better than dynamic arrays on current hardware, maybe except some really atypical applications (huge objects in the nodes?), is when there is a lot of appending and removal done on the ends of the list, or if you need a lot of insertions somewhere in the middle.<p>The wikipedia article on linked lists linked in the article explores a lot of angles and is really excellent. There are some newer, cache-friendlier versions of linked lists, I hope they will start being covered more commonly:<p><a href="http://en.wikipedia.org/wiki/Unrolled_linked_list" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Unrolled_linked_list</a><p><a href="http://en.wikipedia.org/wiki/CDR_coding" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;CDR_coding</a><p>Given linked lists are the workhorse of every Lisp, I wonder if there has been work on using those kinds of data structures in Lisp implementations, or is for example Clojure still using classic linked lists internally.
评论 #7077695 未加载
drcodeover 11 years ago
Certainly a nice intro to Clojure types, though it might be a bit confusing to a beginner, given that linked lists are already built in to Clojure and there&#x27;s nothing I can see in the tutorial saying &quot;You don&#x27;t actually have to do any of this, this is just an exercise it for pedagogical purposes.&quot;
评论 #7077006 未加载
dschiptsovover 11 years ago
Why not just stop at the point that a Pair is just two pointers and a List is a bunch of nested Pairs, without any classes, deftypes or monads?)<p>Btw, the classic trick from SICP lectures, where a Pair was implemented as a closure (a procedure) that accepts messages (to illustrate code-is-data principle) is much more interesting abstraction than this overengineered OO stuff.)
评论 #7080193 未加载
djb_hackernewsover 11 years ago
Pretty indepth stuff, it&#x27;d be useful if you point out why you even need the INode interface, unless I missed that.
评论 #7077453 未加载
评论 #7077480 未加载
bltover 11 years ago
Get rid of that s-t ligature. It&#x27;s distracting.
评论 #7077466 未加载