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›Conditions (if / then / else)

Conditions (if / then / else)

if-then-else is an EXPRESSION: an else is always required and it produces a value.

Simple enough for a 5-year-old to understand

if / then / else

In Haskell, if is an expression: both then and else are mandatory.

Example code
1
main = do
2
let age = 20
3
if age >= 18
Condition.
ConditionCondition.
4
then putStrLn "Adult"
If true.
Output / InputIf true.
5
else putStrLn "Child"
Otherwise (else CONDITION).
Output / InputOtherwise (else CONDITION).
ConditionOutput / Input
💡 Hint: In Haskell, if always requires an else; an isolated if is not allowed.

About this topic

Conditions in Haskell are expressions: they produce values. That's why when you write an if, the else branch is mandatory — a value must be returned even if the condition is not met.

Guards are preferred for cases exceeding three branches. A guard is a list of conditions written alongside the function definition, and the first satisfied branch executes. It is much more readable than writing nested ifs.

Pattern matching, on the other hand, is a way to branch based on the structure of a value. You write separate definitions depending on whether a list is empty or what form a value arrives in. This forms the characteristic structure of Haskell code.

What you will learn

  • the structure of the if expression
  • the necessity of the else branch
  • Guards
  • the case expression
  • Pattern matching

Who is this for?

  • Learners of control flow
  • Those coming from imperative languages
  • Those seeing pattern matching for the first time

When to use?

An if is enough for a quick decision between two options; if there are more than three cases, guards are readable.

If you are branching based on the structure of the value, pattern matching is both safer and the compiler can warn about missing cases.

Common mistakes

Wrong

kontrol :: Int -> String
kontrol n = if n > 0 then "positive"

In Haskell, if is an expression and must produce a value in every case; without an else branch, you get a "parse error". In imperative languages, else is optional because if is a statement, whereas in Haskell it is a value-producing expression.

Correct

kontrol :: Int -> String
kontrol n = if n > 0 then "positive" else "not positive"

Wrong

notu :: Int -> String
notu n
  | otherwise = "passed"
  | n < 50 = "failed"

Guards are tried from top to bottom and since the always-true branch is first, the second branch is never reached; the compiler reports this as a warning. The general branch should always be at the very end.

Correct

notu :: Int -> String
notu n
  | n < 50 = "failed"
  | otherwise = "passed"

Wrong

ilk :: [Int] -> Int
ilk (x:_) = x

Since the empty list case is not defined, when this function is called with an empty list, it stops at runtime saying "Non-exhaustive patterns". When compiler warnings are enabled, it reports this in advance. You need to cover all possible forms.

Correct

ilk :: [Int] -> Maybe Int
ilk (x:_) = Just x
ilk []    = Nothing

Frequently asked questions

Why is else mandatory?

Because if produces a value and every expression has a type. Without else, what value would be returned when the condition is false would remain undefined. In imperative languages, this is not a problem because if is a statement.

What is the difference between guards and case?

Guards branch based on conditions, while case branches based on the structure of the value. They can also be used together: you can write a guard inside a case branch. The choice is made based on whether you need a condition or a structure.

How do I catch missing patterns?

By running the compiler to show all warnings. The missing pattern warning reports an error at compile time that would crash at runtime; keeping this warning enabled is a strong habit in Haskell.

Summary

if is an expression, else is mandatory.

Guards are tried from top to bottom.

The catch-all branch must be at the very end.

Missing patterns produce a runtime error.

Pattern matching branches based on the value's structure.

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