Efficient String Implementation in Haskell

前端 未结 4 1551
旧巷少年郎
旧巷少年郎 2020-12-13 04:51

I\'m currently teaching myself Haskell, and I\'m wondering what the best practices are when working with strings in Haskell.

The default string implementation in Has

4条回答
  •  温柔的废话
    2020-12-13 05:35

    The answer is a bit more complex than just "use lazy bytestrings".

    • Byte strings only store 8 bits per value, whereas String holds real Unicode characters. So if you want to work with Unicode then you have to convert to and from UTF-8 or UTF-16 all the time, which is more expensive than just using strings. Don't make the mistake of assuming that your program will only need ASCII. Unless its just throwaway code then one day someone will need to put in a Euro symbol (U+20AC) or accented characters, and your nice fast bytestring implementation will be irretrievably broken.
    • Byte strings make some things, like prepending to the start of a string, more expensive.

    That said, if you need performance and you can represent your data purely in bytestrings, then do so.

提交回复
热议问题