4 ms·
> String (linked list of Char) That... sounds awful for performance. Is that a real thing?
by jbboehr 4y ago
> String (linked list of Char)
That... sounds awful for performance. Is that a real thing?
- thaumasiotes 4y agoAbsolutely; that's how Erlang represents strings. (Technically, a linked list of integers; there's no such thing as a Char.) It generalizes really well to Unicode; encoding is unnecessary. :p On the other hand, I think the name of the function that converts an integer to its own string representation, integer_to_list, could have been chosen better.
- kaba0 4y agoWhile it is surely bad for performance, it is not that bad in practice because haskell has many optimizations for dealing with these lazy, possibly infinite sequences.
- antonvs 4y agoIt is a real thing, but it's a design decision that dates back to around 1990 when Haskell was very much purely a research language. Haskell's type class capability (similar to traits or interfaces) was still new/experimental at that time, and one benefit of strings as lists is it allowed them to easily be manipulated using the same syntactic and semantic machinery - pattern matching, recursive processing etc. - as other list data. These days real, non-trivial code uses much more optimized string representations, but the original String type still exists and is used in various standard library functions, like "error". But as the original commenter pointed out, "you can just upgrade strings like any other dependency," so if you want, you can always import some other library that uses e.g. the Text type for error message, if you care for some reason.
- crdrost 4y agoYes and it's even worse than you think... The characters are not even raw characters, they are “boxed” into data structures and then the linked-list code guards them with a “thunk,” so “If I have computed this char return the cached char, else compute it and save it to the cache and then return it.” The performance is good enough for teaching and learning and mocking out examples... But the reason for the user libraries is that real string processing workloads need something way better as their default.