If Else in Lisp
Branching with if
and else
in Lisp is straightforward. Here’s how the different constructs work:
Basic
if-else
: In Lisp,if
takes three arguments: the condition, the then-clause, and the else-clause.if
withoutelse
: In Lisp, we usewhen
for this. It executes the body only if the condition is true.Logical operators: Lisp uses
and
andor
for logical operations.Variable declaration before conditional: In Lisp, we use
let
to create local bindings. Thecond
macro is then used for multiple conditions.
Note that in Lisp, you don’t need parentheses around conditions, but the entire expression is enclosed in parentheses.
To run this Lisp program:
Lisp doesn’t have a ternary operator, but the if
special form can be used for simple conditions, and cond
for more complex ones.