An if statement has the form:
The test, then, and else expressions can be any evaluable Lisp expressions -- e.g., symbols or lists. If the evaluation of the test expression returns anything other than nil, e.g. T, 3, FOO, (A S D F), the interpreter evaluates the then expression and returns its value, otherwise it returns the result of evaluating the else expression.(if <test> <then> <else>)
We can use if to define a function to return the absolute difference between two numbers, by making use of the predicate > (greater than). Here it is:
If x is greater than y, then the test, i.e. (> x y), returns T, so the then clause is evaluated, in this case (- x y), which gives the positive difference. If x is less than or equal to y, then the expression (- y x) gets evaluated, which will return 0 or a positive difference.(defun absdiff (x y) (if (> x y) (- x y) (- y x)))
© Colin Allen & Maneesh Dhagat