Testing Conditions Using PicoLisp.
We can use if, ifn, if2 commands of PicoLisp in order to test conditions.Simple if .
It is the simple if-then-else structure. If the condition is true then the first command after the condition will be executed otherwise the second one.: (if (> 2 3) "True" "False")
-> "False"
: (if (< 2 3) "True" "False")
-> "True"
This structure is capable to host many expressions for each possible result of the condition:
(if (< 2 3) (prog (print "h1")(print "h2"))(prog (print "h3")(print "h4")))
"h1""h2"-> "h2"
ifn Means If Not.
If the condition is not True then the first expression is run . Otherwise the second one.
: (ifn (> 2 3) "True" "False")
-> "True"
: (ifn (< 2 3) "True" "False")
-> "False"
(ifn (< 2 3) (prog (print "h1")(print "h2"))(prog (print "h3")(print "h4")))
"h3""h4"-> "h4"
if2 Condition-1 Condition-2 => ((T T)(T F)(F T)(F F)).
We create a function in order to test the command "if2". The function "test-if2" accepts two arguments from the user. Each one is a condition. Afterwards it tests if both, first, second or none of the conditions is true.
: (de test-if2(condition-1 condition-2)
(if2 condition-1 condition-2
"Both are True" "First" "Second" "None" ) )
Testing function calling examples:
: (test-if2 (> 2 3) (< 4 5))
-> "Second"
: (test-if2 (< 2 3) (< 4 5))
-> "Both are True"
: (test-if2 (< 2 3) (> 4 5))
-> "First"
: (test-if2 (> 2 3) (> 4 5))
-> "None"
PicoLisp Commands Used In This Page.
The description of PicoLisp commands used in this page are the following taken from the official PicoLisp documentation (https://software-lab.de/doc/index.html):
(if 'any1 any2 . prg) -> any
any1
evaluates to non-NIL
, any2
is evaluated and returned. Otherwise, prg
is executed and the result returned. See also ifn
, cond
, when
and if2
.
(if2 'any1 'any2 any3 any4 any5 . prg) -> any
any1
and any2
evaluate to non-NIL
, any3
is evaluated and returned. Otherwise, any4
or any5
is evaluated and returned if any1
or any2
evaluate to non-NIL
, respectively. If none of the conditions evaluate to non-NIL
, prg
is executed and the result returned. See also if
and cond
.(ifn 'any1 any2 . prg) -> any
any1
evaluates to NIL
, any2
is evaluated and returned. Otherwise, prg
is executed and the result returned. See also if
, nor
, nand
, unless
and nond
.