Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient String Implementation in Haskell

Tags:

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 Haskell is a list of Char. This is inefficient for file input-output, according to Real World Haskell, since each character is separately allocated (I assume that this means that a String is basically a linked list in Haskell, but I'm not sure.)

But if the default string implementation is inefficient for file i/o, is it also inefficient for working with Strings in memory? Why or why not? C uses an array of char to represent a String, and I assumed that this would be the default way of doing things in most languages.

As I see it, the list implementation of String will take up more memory, since each character will require overhead, and also more time to iterate over, because a pointer dereferencing will be required to get to the next char. But I've liked playing with Haskell so far, so I want to believe that the default implementation is efficient.

like image 259
Rob Lachlan Avatar asked Feb 23 '09 00:02

Rob Lachlan


People also ask

How do you write strings in Haskell?

In Haskell it is also very easiest form of data type which can be used, easy to handle and create as well. String can be created by using double quotes (“”) inside this we can write our string or value that we want string type to be hold for us.

How are strings represented in Haskell?

In Haskell a String is just a list of Char s, indeed type String = [Char] . String is just an "alias" for such list. So all functions you define on lists work on strings, given the elements of that list are Char s.

Are strings lists in Haskell?

Strings are just listsEdit As we briefly mentioned in the Type Basics module, strings in Haskell are just lists of characters. That means values of type String can be manipulated just like any other list.


1 Answers

Apart from String/ByteString there is now the Text library which combines the best of both worlds—it works with Unicode while being ByteString-based internally, so you get fast, correct strings.

like image 88
porges Avatar answered Sep 17 '22 14:09

porges