[Haskell-cafe] Re: Bytestrings and [Char]

Heinrich Apfelmus apfelmus at quantentunnel.de
Tue Mar 23 05:32:16 EDT 2010

Ivan Miljenovic wrote:
> Johan Tibell wrote:
>> A sequence of bytes is not the same thing as a sequence of Unicode
>> code points. If you want to replace String by something more efficient
>> have a look at Data.Text.
> Though Data.Text still has the disadvantage of not being as nice to
> deal with as String, since you can't pattern match on it, etc.
> Whilst it may degrade performance, treating String as a list of
> characters rather than an array provides you with greater flexibility
> of how to deal with it.

Indeed. In particular,  [Char]  and  Data.Text.Text  have different
semantics and performance.

For instance,

    cons :: Char -> Text -> Text

is O(n) whereas  (:)  is O(1). (Not sure whether stream fusion for  cons
 can change that at times.)

Furthermore, certain programs relying on laziness, like

    ahh = 'a' : ahh

    fibs = ' ' : zipWith plus fibs (tail fibs)
        where plus x y = toEnum $ fromEnum x + fromEnum y

are not possible with  Data.Text.Text . (Whether you really need these
is another question, of course.)

Heinrich Apfelmus


More information about the Haskell-Cafe mailing list