Why is Haskell's default string implementation a linked list of chars?

后端 未结 3 535
误落风尘
误落风尘 2021-02-01 19:11

The fact that Haskell\'s default String implementation is not efficient both in terms of speed and memory is well known. As far as I know the [] lists

相关标签:
3条回答
  • 2021-02-01 19:44

    Why is Haskell's default String implementation a singly-linked list

    Because singly-linked lists support:

    • induction via pattern matching
    • have useful properties, such as Monad, Functor
    • are properly parametrically polymorphic
    • are naturally lazy

    and so String as [Char] (unicode points) means a string type that fits the language goals (as of 1990), and essentially come "for free" with the list library.

    In summary, historically the language designers were interested more in well-designed core data types, than the modern problems of text processing, so we have an elegant, easy to understand, easy to teach String type, that isn't quite a unicode text chunk, and isn't a dense, packed, strict data type.

    0 讨论(0)
  • 2021-02-01 20:02

    At this point, it's probably historical: the optimizations that have made things like ByteString so efficient are recent, whereas [Char] predates them all by many years.

    0 讨论(0)
  • 2021-02-01 20:03

    Efficiency is only one axis to measure an abstraction on. While lists are pretty inefficient for text-y operations, they are darn convenient in that there's a lot of list operations implemented polymorphically that have useful interpretations when specialized to [Char], so you get a lot of reuse both in the library implementation and in the user's brain.

    It's not clear that, were the language being designed today from scratch with our current level of experience, the same decision would be made; however, it's not always possible to make decisions perfectly before experience is available.

    0 讨论(0)
提交回复
热议问题