## Useful higher order function for booleans

For the Compleat Fan
Jeremy Dunn
Posts: 95
Joined: Wed Oct 13, 2004 8:02 pm
Location: Bellingham WA

### Useful higher order function for booleans

I love it when I find something simple and useful. One of the things we notice with boolean functions is that they invariably are inside an If...then... expression. Now sometimes we want the truth value of the statement but many times we are only using it to decide whether we are going to use the argument that was input or not. For instance,

Code: Select all

``(if (< x)(setq z x))``
In this case we do not need the truth value itself but only x. Now suppose we wrote the following function

Code: Select all

``````(define-macro (rtn)
(if (eval (args))
(eval (args 1))))``````
Now we could write the previous as (setq z (rtn < x)). This eliminates always having to write the conditional statement. RTN can be used with booleans that take more than one argument. For instance,
(if (= x y)(setq z x)) would be (setq z (rtn = x y)). If the boolean test is true then the first argument of the multiple arguments is returned. The name RTN is an abbreviation of RETURN because the function returns the input when true.

Now we may not be setting the value to a variable but just using it directly in a calculation as in

Code: Select all

``(if (< x y)(* x 4))``
But using RTN this becomes simply (* (rtn < x y) 4). I think you'll agree that this happens quite a lot. Hope you find it useful.