Lesson 4 of 16

Strings

Strings in Haskell

In Haskell, String is an alias for [Char] — a list of characters. This gives you all list operations for free.

Concatenation

Use ++ to concatenate strings:

putStrLn ("Hello" ++ ", " ++ "World!")
-- Hello, World!

Useful Functions

length "hello"        -- 5
reverse "hello"       -- "olleh"
words "foo bar baz"   -- ["foo","bar","baz"]
unwords ["foo","bar"] -- "foo bar"

Converting Numbers to Strings

Use show to convert a number to its string representation:

let msg = "The answer is " ++ show 42
putStrLn msg   -- The answer is 42

Your Task

Build the string "Haskell has 7 letters" using ++ and show, then print it. (Use length "Haskell" to get the count.)

Haskell loading...
Loading...
Click "Run" to execute your code.