WhizzML Reference Manual
2.6 Conditionals
WhizzML offers two conditional expressions, if and cond.
2.6.1 Conditionals with if and when
The if form takes a test expression as its first argument. If it evaluates to any value other than false, the consequent subexpression is evaluated, otherwise the (optional) alternate subexpression is evaluated.
(if <test> <consequent> <alternate>)
If <alternate> is not provided and <test> is false, the whole if expression evaluates to false. But when no alternate is used, it is better to use the special form when , which takes a test and a body (possibly consisting of multiple expressions) that is evaluated only if the test succeeds. Its general form is
(when <test> <body>)
which is equivalent to
(if <test> (prog <body>))
Examples:
(if (> x 3)
(prog (log-info "Creating evaluation")
(create-evaluation {"model" id}))
(log-warn "No evaluation available"))
(if (> x 0)
(/ 42 x)
(if (< x 0)
(/ -33 x)
x))
(when (> x 0)
(log-info "Positive case")
(handle-positive-x x))
2.6.2 Conditionals with cond
WhizzML also offers a compact way of performing conditional code execution based on an arbitrary list of tests, using the cond keyword. The general syntax follows the pattern:
(cond <test1> <consequent1>
<test2> <consequent2>
...
<testn> <consequentn>
<alternate>)
which is equivalent to a sequence of if expressions performing the same tests in the same order:
(if <test1>
<consequent1>
(if <test2>
<consequent2>
(if ....
....
(if <testn>
<consequentn>
<alternate>) ...) ...))
In words, every test is evaluated in order until one of them yields a value different from false, in which case the full cond evaluates to the associated <consequent> subexpression. If all tests fail, the result of the expression is the result of evaluating the final <alternate> subexpression.
Examples:
(cond false 3
false 4
42) ;; => 42
(cond (> x 3) "big"
(< x 3) "small"
"medium")
2.6.3 Logical and
(and <exp1> ... <expn>)
The and special syntax evaluates each of its arguments in turn until one of them yields a false value, which is then the result of the whole expression. The rest of the arguments are not evaluated, i.e., the form is short-circuited (which is the reason this is a special form, not definable as a user procedure).
(and (> 23 0) "foo" "bar") ;; => "bar"
(and (> x 0) (< (/ y x) 0.002))
2.6.4 Logical or
(or <exp1> ... <expn>)
The or special syntax evaluates each of its arguments in turn until one of them yields a truish value, which is then the result of the whole expression. The rest of the arguments are not evaluated, i.e., the form is short-circuited (which is the reason this is a special form, not definable as a user procedure).