I wanted to write my own if
using Boolean logic and macros. I came up with the following implementation:
(defmacro -if (condition if-true if-false)
"Implements `if` in terms of Boolean logic only"
`(or (and ,condition ,if-true)
(and (not ,condition) ,if-false)))
I tested it manually on several cases and it works as expected. But then I wrote a simple test function to perform a series of tests and got one result I still cannot understand. I wrote the function like this:
(defun -if-test ()
(let ((passed 0)
(test-format "TEST: ~50a ==> ~:[FAILURE~;SUCCESS~]~%")
(cases '((return-value (> 2 1) true)
(return-value (< 2 1) false)
(standard-output (> 2 1) "true")
(standard-output (< 2 1) "false"))))
(dolist (test-case cases)
(destructuring-bind (type test expected) test-case
(let ((result (case type
(return-value
(eq (-if test 'true 'false) expected))
(standard-output
(with-output-to-string (out)
(string= (-if test (print "true" out) (print "false" out)) expected)))
(otherwise (error "Unknown test type: ~a" type)))))
(when result (incf passed))
(format t test-format test-case result))))
(format t "Result: ~a/~a tests passed.~%" passed (length cases))))
When I run the tests I get the following output:
TEST: (RETURN-VALUE (> 2 1) TRUE) ==> SUCCESS
TEST: (RETURN-VALUE (< 2 1) FALSE) ==> FAILURE
TEST: (STANDARD-OUTPUT (> 2 1) true) ==> SUCCESS
TEST: (STANDARD-OUTPUT (< 2 1) false) ==> SUCCESS
Result: 3/4 tests passed.
NIL
The second failing case apparently gives different results when run by hand than when run as part of this function. I tried debugging it with SLDB and indeed the result is different from standalone execution. I suspect I missed some crucial execution detail or something like that. Can someone explain to me what happens here? Help really appreciated.
P.S. My implementation is Clozure CL.