-
Notifications
You must be signed in to change notification settings - Fork 4
0011: Email Tutorials Haskell For Beginners – Boolean Logic Basics in Haskell
- 11.1. Boolean Values in Haskell
- 11.2. Equality and Inequality Operators
- 11.3. Comparison Operators
- 11.4. Logical AND (
&&) and Logical OR (||) - 11.5. Truth Tables and Combined Expressions
- 11.6. Boolean Negation with
not - 11.7. A Walkthrough of a More Complex Boolean Expression
- 11.8. Glossary of Terms for This Lesson
Boolean logic answers questions about what is true and what is false. In Haskell, the type responsible for Boolean values is:
Bool
Bool has exactly two valid values: True and False. These two values are at the core of all logical operations in Haskell. Every comparison, conditional, or logical combination ultimately produces a Bool.
The first category of Boolean functions involves testing equality.
The operator == compares two values and returns True if they are equal, False otherwise.
Examples:
-
2 == 2→True -
"hello" == "world"→False
The operator /= checks for not equal, returning True when values differ.
Examples:
-
3 /= 3→False -
2 /= 3→True
The symbol resembles the mathematical “≠” sign, which isn't available on keyboards, so Haskell uses a forward slash plus equals.
The second category involves ordering comparisons, which apply to values that have a meaningful order—numbers, characters, strings, etc.
Common comparison operators:
| Operator | Meaning |
|---|---|
< |
less than |
<= |
less than or equal |
> |
greater than |
>= |
greater than or equal |
Examples from the lesson:
-
512 < 1024→True -
1.24 <= 2→True(the transcript incorrectly said False, but mathematically it is True) -
256 > 32.25→True -
"a" >= "b"→Falsebecause"a"comes before"b"alphabetically
All these comparisons yield Bool values.
Once you produce Booleans, you can combine them:
- Only
True && Trueresults inTrue. - Any
Falsemakes the whole expressionFalse.
- Only
False || Falseresults inFalse. - Any
TrueproducesTrue.
These operators allow you to build complex Boolean conditions.
To reason about combinations, a truth table is useful.
For variables x and y, each being True or False:
| x | y | x && y | x || y | | ----- | ----- | ------ | ------ | | True | True | True | True | | True | False | False | True | | False | True | False | True | | False | False | False | False |
This table helps you predict what an expression involving AND/OR will evaluate to.
The final Boolean operation covered is negation.
not True -> False
not False -> True
The not function takes a single Bool and flips it.
The lesson concludes with more challenging examples to test understanding.
m = not (True && j)
Given that j is False:
-
True && False→False -
not False→True
So m = True.
n = not False && True
Step-by-step:
-
not False→True -
True && True→True
So n = True.
o = False || not (not h) || not i
Given:
h = Falsei = True
Evaluate:
-
not h→True -
not (not h)→False -
not i→False
Expression becomes:
False || False || False
So:
o = False
These evaluations show how combining AND, OR, and NOT—with nested parentheses—can produce increasingly complex expressions, all reducible step-by-step.
-
Bool The Boolean type in Haskell with exactly two values:
TrueandFalse. -
Equality operator (
==) ReturnsTruewhen two values are equal. -
Inequality operator (
/=) ReturnsTruewhen two values differ. -
Comparison operators (
<,<=,>,>=) Operators used to compare orderable values, producingBool. -
Logical AND (
&&) ProducesTrueonly when both operands areTrue. -
Logical OR (
||) ProducesTruewhen at least one operand isTrue. -
Negation (
not) Flips a Boolean value. -
Truth table A table showing the results of Boolean operations for all input combinations.
-
Orderable types Types with a defined ordering, such as numbers, characters, and strings.
-
Boolean expression Any expression whose final result is a
Bool.
Bernard Sibanda is a global Technology Entrepreneur, Web3 and Software Consultant with a deep focus on Cardano Blockchain, Midnight and Community building.
Key Positions:
- Founder, CTO, Developer Advocate cohort #1, Fullstake Developer, Cardano Ambassador, Catalyst Project Manager, DREP-WIMS:
- Co-founder of ABL Tech and Cardano Africa Live
- EBU-certified Plutus Pioneer (Plutus/Haskell)
- Cohort #1 Plutus Pioneer Developer
- Catalyst Community Reviewer & Funded Projects Manager
-
DRep for WIMS-Cardano (ID:
drep1yguj8zu48n99pv70yl6ckzt9hdgjy8yjnlqs2uyzcpafnjgu4vkul) - Intersect Developer Advocate
- Intersect Committe Member 2025-2026
- Cardano Marketer,Promoter and blogger
- Cardano Open Source Contributor
- Cardano communities and events organizer and builder
- Cardano Ambassador for South Africa
Official links:
- Stablecoins Dex
- Coxygen Global Universities
- WIMS Cardano Global
- Cardano Africa Live
- WIMS Cardano Videos
- Cardano Smart Contract Videos
- Fullstack IT Consulting
Social links: