12 ms·
Most other languages don't use singly linked lists to begin with. Because they suck.
by TylerE 7y ago
Most other languages don't use singly linked lists to begin with.
Because they suck.
- cryptonector 7y agoIn Haskell, because of laziness, lists work well enough, but they're really just a thunk container. Conduits are better.
- Retric 7y agoThe way modern memory works linked lists are practically a denial of service attack on system performance. IMO, like goto’s it’s better to just forget they exist.
- jerf 7y agoLinked lists stink as a manifested in-memory data structure. However, if what your linked list is is "{concrete value} : {code to generate the rest of the list}" and you walk it once, never having the whole thing at once, it isn't necessarily bad. That is the point cryptonector is making. The memory inefficiency of a linked list doesn't matter if you never really have it "in memory". That said, the ready availability of linked lists in Haskell does mean that you end up with rather more strictly-manifested linked lists than you might like, since any of them can be manifested as actual linked lists. If you're using Haskell though, while you haven't exactly completely given up on that sort of performance, it must not be foremost on your mind.
- cryptonector 7y ago> Linked lists stink as a manifested in-memory data structure. However, if what your linked list is is "{concrete value} : {code to generate the rest of the list}" and you walk it once, never having the whole thing at once, it isn't necessarily bad. That is the point cryptonector is making. The memory inefficiency of a linked list doesn't matter if you never really have it "in memory". Yes, exactly. A lazy list is just another term for generator. If you hold on to a reference to the head of such a list as you run the generator, then you'll consume a lot of memory and will end up with a strict list, and also you'll be sad. But if you don't hold on to that reference, then it will be very efficient. > That said, the ready availability of linked lists in Haskell does mean that you end up with rather more strictly-manifested linked lists than you might like, since any of them can be manifested as actual linked lists. If you're using Haskell though, while you haven't exactly completely given up on that sort of performance, it must not be foremost on your mind. Especially strings as lists of characters. That was a mistake. Well, in some sense, character data has to be a list, since with Unicode you really can't efficiently index a string... unless it's a rope and you apply the sorts of tools that xi does (e.g., keeping track of character counts per rope segment, etc.). But strings should be sufficiently opaque that arrays, lists, and ropes are all equally able to implement their semantics.
- Retric 7y agoThat’s frankly not a linked list.
- Tarean 7y agoHaskell is lazy, all data structures work this way unless specifically annotated otherwise. Within standard haskell 2010 it is hard to declare a linked list that doesn't work this way. It is also worth noting that haskell has library specified fusion rules so the closure isn't even allocated most of the time. So sum (take 10 [0..]) compiles into an allocation free loop. I will give you that this isn't a linked list anymore, though.
- jerf 7y agoHaskell has a duality between flow control and data structures as a result of its laziness. It is a perfectly valid point of view to look at such a thing as a control structure, rather than a data structure. Technically, if you want to really understand how the "IO monad" does its thing, this is actually how it works. Technically your program declares a probably-infinite data structure that contains all possible paths your code could take, e.g., if you have a "if user input is negative then do X else do Y", what you really have is a data structure representing the if-then clause. What keeps the infinite data structure from being manifested in RAM is that the whole thing is resolved lazily, so only one branch will be actually evaluated. That's why the "IO monad" is actually pure; it simply "purely" creates a huge data structure that is resolved by an interpreter later. In the theoriest of theories, an IO value in Haskell really doesn't execute anything either and the whole language is pure... it is only the interpretation of the IO value that finally interfaces with the real world. It isn't always the most helpful perspective and you can become an expert Haskell programmer without ever looking at your program this way, but technically, under the theory hood, that's what's happening.
- cryptonector 7y agoIn Haskell lazy values are just thunks. So a Lisp-style cons can have a tail that's just a thunk, and when you need to coerce it to a non-thunk value, it just happens and presto, you have the next cons. That's a list! A lazy list.
- 7y ago
- sullyj3 7y agoDue to GHC's fusion, they're often not even created. They're used more like control flow constructs than arrays.
- hope-striker 7y agoRight, I mostly end up using Haskell's lists as an extremely ergonomic counterpart to iterators / generators in languages like Python and Rust.
- dnautics 7y agoThat's really not true. Whole web frameworks are built strictly around linked list structures that are far more robust, performant and DOS-proof than anything you'll write in your lifetime.
- TylerE 7y ago<citation needed> The majority of langauges web frameworks are written in do not have linked lists, at all. Python doesn't. Ruby doesn't. Java doesn't.
- uramoron 7y ago> Java doesn't https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html https://docs.oracle.com/javase/7/docs/api/java/util/LinkedLi...
- dnautics 7y agoBasically, anything written in Elixir Phoenix, or erlang, so: Discord, Pagerduty, Bet365, the entire NHS. If you're a startup, in SF, there's a good chance your payments goes through Brex. You might be using Slab for your documentation, you might use Boston's MBTA, etc. Probably a not-insignificant chunk of what you do as a startup at some level depends on a framework that uses linkedlists. Do you use RabbitMQ or Riak? Linked lists are the primary list datatype for that system.
- adwn 7y ago> The way modern memory works linked lists are practically a denial of service attack on system performance Wrong. Intrusive, doubly-linked lists are extremely performant and useful in the right places, especially if you have callbacks which store a pointer to a list element (this often happens with device drivers in low level programming), or if the elements can belong to several lists. The Linux kernel, for example, makes plenty use of linked lists. > IMO, like goto’s it’s better to just forget they exist. Wrong again. In a language without automatic memory management and without scoped destruction (cough C cough), they are immensely useful. Again, refer to the Linux kernel. Or any competently written, non-trivial C program for that matter. Note: I'm not defending C here, I'm defending correctly used gotos in C programs.
- overgard 7y agoI don't think you can really get around linked lists having bad cache locality even if they're intrusive. The point of arrays usually being faster is that they're much more cache friendly, and fitting data in cache and avoiding branch mispredictions is usually much more important than o(1) insert/delete.
- nine_k 7y agoIf you allocate all the nodes of a particular list from one arena, the cache performance should be OK. IDK if this is the case for device drivers, but AFAICT device drivers tend to preallocate memory on loading and manage it locally.
- didibus 7y agoArn't there hybrid array lists? Like where instead of growing the array when it goes beyond bound, the last slot is reserved to create a link to the next array chunk?
- adwn 7y agoLinked lists fare worse than arrays only for sequential iteration in tight loops. That includes many/most cases, but not all. As for O(1) insertion/deletion: It's not always about amortized cost, sometimes you actually do care about worst case execution time. Look, I'm not saying that linked lists can replace arrays. In fact, I almost always use arrays instead of linked lists. What I'm arguing against is the notion that arrays always trump linked lists, which is false, and repeating that meme shows a lack of deeper understanding.
- fiddlerwoaroof 7y agoThis is only true if you’re writing code that’s compute bound: in 90% of the production code I’ve seen, the “inefficiency” of a singly-linked list is the absolute last thing that I worry about.
- Retric 7y agoO(N) random reads and O(N) random inserts can quickly turn a wide rage of things to be compute bound. Creating a linked list from scratch via random insertions for example is an N^2 operation with surprisingly large constants. Now sure, if it’s small and you’re only creating then reading it once then that’s fine. But, you’re assumptions need to be accurate or you just created a problem.
- fiddlerwoaroof 7y agoIn most of the cases where I’m using a list or array, I’m adding items to one end and then iterating forward over the structure a handful of times. But, yeah, if you’re relying heavily on random access, pick a vector/array or some kind of tree/hash table.
- vmchale 7y ago> IMO, like goto’s it’s better to just forget they exist. They're quite useful in functional languages. In Haskell in particular they can have nice performance effects if you use them sensibly.
- danharaj 7y agoEvery use of conduit in production that I have witnessed has been a mistake.
- cryptonector 7y agoOh? Can you elaborate?
- vmchale 7y ago> Conduits are better. That's for streaming I/O. Lazy linked lists are appropriate for streaming without side effects
- smabie 7y agoI mean, they might suck, but that's not really true that most languages don't use them. I can think of a lot that use them as their default data structure (OCaml, Haskell, Scala, Common Lisp, Racket, Clojure, etc). They're nice with recursion or if you don't know the size of your data ahead of time. But honestly, I've only used recursion a handful of times in my entire career, despite working almost exclusively with functional languages.
- didibus 7y agoCorrection for Clojure. It doesn't use singly linked list by default. It uses something more akin to an ArrayList, Clojure calls it Vector. Elements are indexed, and have pseudo O(1) lookup, and it adds to the end in pseudo O(1) as well.
- ninjapenguin54 7y agoCorrection: Clojure has linked lists and vectors. Haskell has both too. https://clojure.org/guides/learn/sequential_colls https://clojure.org/guides/learn/sequential_colls https://hackage.haskell.org/package/vector-0.12.1.2/docs/Data-Vector.html https://hackage.haskell.org/package/vector-0.12.1.2/docs/Dat... Don't blame the language, & learn to use the right data structure for the job.
- didibus 7y agoParent was talking about default data-structure, and for Clojure Vectors are the default "list" data-structure, even though it has other type of lists. Not trying to throw any of the other languages mentioned under the bus, I do not know them well enough to know what list structure they default too or more idiomatically rely on.
- fulafel 7y agoIn Clojure, most standard functions return lazy sequences, and in code people usually use vectors which are not linked lists. But it does of course have the classic Lisp list as well.
- dnautics 7y agoSingly linked lists let you do fast immutable list copies on entry into a new function context. In python, for example let's say: def f: arr = [0, 1, 2] g(arr) return arr[0] What does f return? You don't know. It depends on g. If g has other dependencies, and someone changes a deep dependency (or maybe a library changes an implementation) you could completely fuck up the result of f, spooky action at a distance, and all of your code could be hosed. Languages that default to using linked lists do not have this problem.
- didibus 7y agoI'm not following why singly linked list let you do fast immutable list copies? You saying before the call to "g", you would make a deep copy of "arr" and that's faster to do with a singly linked list? Could you explain why?
- mrkeen 7y agoIf it's immutable there's no need to copy.
- didibus 7y agoThat's true of any data-structure though.
- tom_mellior 7y ago"Copying" an immutable singly-linked list is just copying the reference to the first element. That is, the entire list is really shared, not actually copied element by element like an array would have to be. But since it is immutable, this doesn't matter: You cannot change it in a way that would mess up other users' view of the list. What you can still do is cons new elements to the front of your copy. No other copy of the list will be affected. Because the list is singly linked, other owners of references into the list cannot walk back to observe your elements. They can even cons their new elements to the "same" list without any conflict. All this is very useful if you want to process something with a stack, such that sub-operations can push elements to the stack that they have to remove again before returning. Pushing x and y to the stack is just constructing the list y : x : stack. Popping y and x from the stack when you're done is a no-op: "stack" is still a reference to the stack as it was before these operations.