Skip to content
Lesson 4 of 15

Step 1 of 5 · Reading · ~1 min

Learn

Strings and Show

String in Haskell is a list of Char: type String = [Char]. So all list operations work on strings.

greeting :: String
greeting = "hello, world"

length greeting           -- 12
map toUpper greeting      -- "HELLO, WORLD" (need import Data.Char)
reverse greeting          -- "dlrow ,olleh"
greeting ++ "!"           -- "hello, world!"  (++ is concat)
head greeting             -- 'h'
take 5 greeting           -- "hello"

For performance-critical text, use Data.Text (the standard for real apps) or Data.ByteString. Plain String is fine for scripts and learning.

No string interpolation in standard Haskell. Concatenate with ++ or use printf-style formatting from Text.Printf:

import Text.Printf
printf "%s is %d years old\n" name age
Up nextShow and ReadStrings and Show

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…