All cases must be covered, using a placeholder at the end of the function definition will cover all other possible cases. Cases are checked from top to bottom of function definition, so the wildcard entry should be at the bottom.
This wildcard matching pattern works very well as you can define functions very cleanly:
_ represents a generic variable in function definitions, something that we don’t care about.
++ is used for concatenation of lists
As patterns allow you to refence a whole list easily when you’ve broken it down into constituent elements in a function
name@(list), name is used to refer to the
list is usually written like
easy access to the first and second items of the list.
Guards are used for checking whether parameters satsify some predicate, they’re like having a whole load of if and else
statements, just must much prettier,
| marks the start of a guard,
\n ends it:
otherwise is used (It’s a wildcard matcher) and will handle anything that doesn’t match the predicates above it.
Guards are evaluated like patterns, from top to bottom.
In python you’d calc something once like:
x = x * 10.0/13.0, then use x for all our further calculations. Haskell
doesn’t have variable, only functions, or only immutable variables, depending on how you want to think of it.
It’s very simple really, don’t overcomplicate it in your head. Just make sure all vars are located in same column like above. The vars lexical scope is the function.
let expressions are very local, they’re pretty much self-contained:
let <bindings> in <expression>, they can be used anywhere
other expressions can be used. The use of
where just binds values to names whereas
let actually is an expression itself.
case expressions follow the pattern:
A more concrete example: