concept string in category haskell

appears as: String, strings, String, string, strings
Haskell in Depth

This is an excerpt from Manning's book Haskell in Depth.

This can be done easily by concatenating strings and data converted to String values with show:

parseIPRange :: String -> Maybe IPRange
parseIPRange = guarded (2 `isLengthOf`) . splitOn ","
               >=> mapM parseIP
               >=> listToIPRange
  where
    listToIPRange [a,b] = pure (IPRange a b)
    listToIPRange _ = empty
Get Programming with Haskell

This is an excerpt from Manning's book Get Programming with Haskell.

You need to build a function that will take a name tuple (as you used before in the sorting example) and an office location and then put together the mailing address for you. A first pass at this function might look like the following. The only other thing we need to introduce that you haven’t seen yet is the ++ operator used to concatenate strings (and lists).

So far in this book, you’ve made heavy use of the String type. In the preceding lesson, you saw that you can even view an I/O stream as a lazy list of type Char, or a String. String has been useful in helping you explore many topics in this book. Unfortunately, String has a huge problem: it can be woefully inefficient.

From a philosophical standpoint, nothing could be more perfect than representing one of the more important types in programing as one of the most foundational data structures in Haskell: a list. The problem is that a list isn’t a great data structure to store data for heavy string processing. The details of Haskell performance are beyond the scope of this book, but it suffices to say that implementing Strings as a linked list of characters is needlessly expensive in terms of both time and space.

In this lesson, you’ll take a look at a new type, Text. You’ll explore how to replace String with Text for more-efficient text processing. Then you’ll learn about the functions common to both String and Text for processing text. Finally, you’ll learn about how Text handles Unicode by building a function that can highlight search text, even in Sanskrit!

The first thing you need to do is learn how to use the Text type. Data.Text has two functions, pack and unpack, which can be used to convert String -> Text and Text -> String. Figuring out which function does what can easily be determined by their type signatures:

T.pack :: String -> T.Text
T.unpack :: T.Text -> String

Here are some examples of converting a String to Text and back again.

23.1.1. When to use Text vs. String

In the commercial Haskell community, Data.Text is strongly preferred over String. Some members of the Haskell community argue that the standard Prelude should be thrown out for anything practical due to the heavy dependency on String. While learning Haskell, String is useful for two reasons. First, as mentioned, many of the basic string utilities are baked into the standard Prelude. Second, lists are to Haskell what arrays are to C. Many concepts in Haskell are nicely demonstrated with lists, and strings are useful lists. For learning purposes, feel free to stick with String. But for anything beyond exercises, use Data.Text as much as possible. You’ll continue to use String in many places in this book but will start to use Data.Text more often.