🎓 A1Learner
🔍
0 of 54 lessons completed

Haskell Strings

A string is a value that holds text. Haskell writes text inside double quotes.

++ joins two strings

The ++ operator joins the second string to the end of the first one.

main :: IO ()
main = putStrLn ("Hello, " ++ "Ana")

length counts the characters

length counts every character in the string. The word Haskell has seven letters.

main :: IO ()
main = print (length "Haskell")

print keeps the double quotes

print writes a string in its shown form. The shown form keeps the double quotes.

main :: IO ()
main = print "Hello"

Example

main :: IO ()
main = print (length ("Ana" ++ " Verma"))

Key points

  • String: a String value holds text.
  • Quotes: text is written between double quotes to make a string.
  • Joining: the ++ operator joins two strings into one longer string.
  • length: length counts the characters in a string.
  • Empty string: the value "" is a string with no characters.

In this example, ++ joins "Ana" and " Verma" into one string, length counts its nine characters, and print writes 9.

Note

print shows a string with double quotes around it.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Haskell Playground

Exercise

What does print (length "code") print?