Conditions

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
Conditional execution: If the condition any1 evaluates to non-NILany2 is evaluated and returned. Otherwise, prg is executed and the result returned. See also ifncondwhen and if2.

(if2 'any1 'any2 any3 any4 any5 . prg) -> any
Four-way conditional execution for two conditions: If both conditions any1 and any2 evaluate to non-NILany3 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-NILprg is executed and the result returned. See also if and cond.



(ifn 'any1 any2 . prg) -> any
Conditional execution ("If not"): If the condition any1 evaluates to NILany2 is evaluated and returned. Otherwise, prg is executed and the result returned. See also ifnornandunless and nond.