News for hackers. Stuff that matters.
Re:I Love OCaml by kristianp on Friday November 07, 2025 @03:40PM (#45850856)
> * Like all FP languages it has a weird obsession with singly linked lists, which are actually a pretty awful data structure

This made me chuckle. I've had that thought before, shouldn't the default be a vector on modern devices? Of course other collection types are available.

Re:I Love OCaml by olivia-banks on Friday November 07, 2025 @04:19PM (#45851229)
I'd say that functional programming is probably one of the only domains where linked lists actually make sense. Vectors certainty have their use in more places though.
Re:I Love OCaml by spooky_deep on Friday November 07, 2025 @05:39PM (#45852057)
A good compiler will make the lists disappear in many cases. No runtime overhead. I actually love single linked lists as a way to break down sequences of problem steps.
Re:I Love OCaml by int_19h on Friday November 07, 2025 @05:41PM (#45852083)
Why would a specific way of structuring data in memory be relevant to breaking down sequences of problem steps?

If what you mean is the ability to think in terms of "first" and "rest", that's just an interface that doesn't have to be backed by a linked list implementation.

Re:I Love OCaml by olivia-banks on Friday November 07, 2025 @07:03PM (#45852777)
I’m curious what you mean. Surely there’s the overhead of unpredictable memory access?
Re:I Love OCaml by deredede on Saturday November 08, 2025 @01:30AM (#45854614)
Not GP but bump allocation (OCaml's GC uses a bump allocator into the young heap) mitigates this somewhat, list nodes tend to be allocated near each other. It is worse than the guaranteed contiguous access patterns of a vector, but it's not completely scattered either.
Re:I Love OCaml by kragen on Friday November 07, 2025 @11:54PM (#45854252)
In Haskell, yes, because laziness permits deforestation. ML, including OCaml, is eager and consequently cannot do this.
Re:I Love OCaml by MrJohz on Saturday November 08, 2025 @12:03AM (#45854289)
I believe it's possible in theory - Koka has a whole "functional but in-place" set of compiler optimisations that essentially translate functional algorithms into imperative ones. But I think that's also possible in Koka in part because of its ownership rules that track where objects are created and freed, so might also not be feasible for OCaml.
Re:I Love OCaml by kragen on Saturday November 08, 2025 @12:31AM (#45854411)
I mean, the set of valid deforestation transformations you could do to an OCaml program is not literally the empty set, but OCaml functions can do I/O, update refs, and throw exceptions, as well as failing to terminate, so you would have to be sure that none of the code you were running in the wrong order did any of those things. I don't think the garbage collection issues you mention are a problem, though maybe I don't understand them?
Re:I Love OCaml by MrJohz on Saturday November 08, 2025 @02:07AM (#45854767)
Part of what Koka's functional-but-in-place system relies on is the Perceus program analysis, which, as I understand it, is kind of like a limited Rust-like lifetime analysis which can determine statically the lifetime of different objects and whether they can be reused or discarded or whatever. That way, if you're, say, mapping over a linked list, rather than construct a whole new list for the new entries, the Koka compiler simply mutates the old list with the new values. You write a pure, functional algorithm, and Koka converts it to the imperative equivalent.

That said, I think this is somewhat unrelated to the idea of making linked lists disappear - Koka is still using linked lists, but optimising their allocation and deallocation, whereas Haskell can convert a linked list to an array and do a different set of optimisations there.

See: https://koka-lang.github.io/koka/doc/book.html#sec-fbip

Re:I Love OCaml by otabdeveloper4 on Saturday November 08, 2025 @01:04PM (#45858612)
Ah yes, our old friend - the sufficiently smart compiler.
Re:I Love OCaml by int_19h on Friday November 07, 2025 @05:40PM (#45852067)
The reason why functional languages like linked lists so much is because they are very easy to make immutable without a lot of pain all around. If you implement an immutable vector in a straightforward way, though, you basically need to do a lot of copying for any operation that needs to construct one (the rigmarole with immutable strings, and existence of hacks such as StringBuilder, is a good illustration of the problem).

But you can have a data structure that is more like vector under the hood while still supporting efficient copy-with-modifications. Clojure vectors, for example.

Re:I Love OCaml by gigatexal on Saturday November 08, 2025 @02:19AM (#45854820)
That makes sense. But LinkedIn lists are horrible for cache efficient things no? I think there was an article on HN from a Golang perspective padding structs or something such that they are efficient wrt to cache hits.
Re:I Love OCaml by rkomorn on Saturday November 08, 2025 @02:26AM (#45854846)
> But LinkedIn lists are horrible for cache efficient things no?

LinkedIn Lists you say? (Sorry. (But not that sorry.))

Edit meta sidenote: in this modern meme world, it's virtually impossible to know if a typo is a typo or just a meme you haven't heard of yet.

Re:I Love OCaml by gigatexal on Saturday November 08, 2025 @04:31AM (#45855438)
lol my phone autocorrected ugh.

But you know what I mean. Some grace would be nice.

Re:I Love OCaml by rkomorn on Saturday November 08, 2025 @05:08AM (#45855605)
I know. It was just too hard to pass up. :)
Re:I Love OCaml by gigatexal on Saturday November 08, 2025 @06:18AM (#45855889)
Well you got me. I audibly groaned and slapped my forehead.
Re:I Love OCaml by stirfish on Saturday November 08, 2025 @01:45PM (#45858915)
LinkedIn lists will tell you they contain one new element, but you have to click on a link and scroll through their suggested new elements to see what it is
Re:I Love OCaml by rkomorn on Saturday November 08, 2025 @01:48PM (#45858936)
Do they also always end with an element that tells you what the LinkedIn list taught it about B2B sales?
Re:I Love OCaml by mrkeen on Saturday November 08, 2025 @05:32PM (#45860647)
They are, but you don't tend to keep them around in memory long enough. Start consuming the front before you produce the back.
Re:I Love OCaml by dontlaugh on Saturday November 08, 2025 @03:44AM (#45855191)
Which is a pity, you can fit entire small arrays in a cache line to end up with no pointer chasing at all.