Syntactic Sugar I: if and case
Explore how to write more readable Haskell functions using if-expressions for conditional logic and case expressions for pattern matching. Understand how these syntactic sugars improve clarity without adding new capabilities, while mastering proper indentation and multi-line layout techniques.
We'll cover the following...
In the next two lessons, we explore a few more syntax features of Haskell. These features can be considered syntactic sugar – they do not allow us to express anything we could not do with our current repertoire of syntax constructs. However, they allow the expression of certain functions in a more readable way. Most of them make use of Haskell’s layout syntax, i.e., they span several lines and need to be indented appropriately. We start with some variants of conditional expressions and matching.
If-expressions
If-expressions are conditional expressions that have a syntax similar to if-statements in other programming languages. The difference is that they are not statements, but expressions.
An if-expression has the form
if condition then ...