If Else in Idris
Our first example demonstrates branching with if
and else
in Idris. This is straightforward and similar to many other programming languages.
To run this program, save it as IfElse.idr
and use the Idris compiler:
Note that in Idris, you don’t need parentheses around conditions, but indentation is significant. The then
and else
keywords are required for each branch.
Idris doesn’t have a ternary operator, so you’ll need to use a full if
statement even for basic conditions. However, Idris has pattern matching which can often be used as an alternative to complex if-else structures.
One key difference in Idris is its strong type system and support for dependent types. While this example doesn’t showcase these features, they allow for more powerful and precise conditionals in more complex programs.