Reading — step 1 of 5
Learn
~1 min readStrings 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
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…