HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to HaskellVariables (let)Data TypesOperatorsStringsListsConditions (if / then / else)Iteration (map / sum)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Value & Type
🔢 Numbers & Math
✍️ String
📚 List
🔀 Condition
🧩 Function
Haskell›Strings

Strings

Length (length), concatenation (++), capitalization (map toUpper), and searching.

Simple enough for a 5-year-old to understand

Length and uppercase

A string is actually a list of characters. length gives the length. For uppercase, map toUpper (Data.Char).

Example code
1
import Data.Char (toUpper)
Required for toUpper.
ImportRequired for toUpper.
2
main = do
3
let s = "HexLibrary"
4
print (length s)
10
Output / Input10
5
putStrLn (map toUpper s)
HEXLIBRARY (capitalize every letter)
Output / InputHEXLIBRARY (capitalize every letter)
ImportOutput / Input
💡 Hint: map toUpper s capitalizes every letter of the text.

About this topic

In Haskell, the string type is actually a list of characters. This means that all functions that work for lists also work on strings: length, reversal, filtering, and mapping are all the same tools.

The price of this design is performance. A character list is inefficient for large texts; in real-world applications, the Text type is preferred. While the default type is enough when learning, it is necessary to know this distinction.

Haskell does not automatically convert numbers to strings. When you want to include a number in a text, you must explicitly write the conversion. This is the most common type error encountered by people coming from other languages.

What you will learn

  • The consequences of strings being lists
  • Concatenating text
  • The conversion between numbers and strings
  • Length and slicing operations
  • The choice of string type

Who is this for?

  • Those writing Haskell code that processes text
  • Those receiving type errors
  • Those experiencing performance issues

When to use?

For learning and small scripts, the default string type is sufficient.

In real-world applications processing large texts, switching to the Text type makes a noticeable difference in terms of memory and speed.

Common mistakes

Wrong

main = putStrLn ("Toplam: " ++ 5)

The concatenation operator expects two lists of the same type; a number is not a list of characters, resulting in a type error. You must convert the number to a string first. Haskell never performs this conversion automatically.

Correct

main = putStrLn ("Total: " ++ show 5)

Wrong

main = do
  let n = read "42"
  print (n + 1)

The compiler cannot know what type the read value will be and gives an ambiguous type error. The read function infers the target type from the context; when it cannot infer it, you must specify it explicitly. This is an example of the limits of Haskell's type inference.

Correct

main = do
  let n = read "42" :: Int
  print (n + 1)

Wrong

main = putStrLn (head "")

An empty list has no first element, and the program crashes at runtime saying "Prelude.head: empty list". The type system cannot catch this error because emptiness is not part of the type. The safe way is to use pattern matching or safe versions.

Correct

main = case "" of
  (c:_) -> putStrLn [c]
  []    -> putStrLn "empty"

Frequently asked questions

Why is the string defined as a list?

For the simplicity of the language: no separate string type and separate functions are needed, list tools work directly. The price is performance, which is why packed string types are used in production code.

How do I convert text to a number?

The read function is used, but it throws an error at runtime on invalid input. The safe way is to use the version that attempts to read and returns an empty result on failure; this should be preferred for user input.

How to write multi-line strings?

In standard Haskell, line breaks are written with an escape sequence. For longer texts, putting lines in a list and joining them is a common method; real multi-line strings are also possible with language extensions.

Summary

A string is a list of characters.

Concatenation is done with the list operator.

Explicit conversion is required to turn a number into a string.

The type of the read value must be specified.

Accessing the first element of an empty list throws an error.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code