Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Scheme, can if be expressed as a combination of boolean operators?

It is easy to express and, or, and not in terms of if (with an assist from a local binding for or). I'd like to know if the reverse is true. My naïve first attempt:

(if test conseq altern) => (or (and test conseq) altern)

However, if test is non-#f and conseq is #f, the translation evaluates to altern, which is incorrect.

Is there a translation that evaluates to the correct value while maintaining the short-circuit nature of if?

like image 913
acfoltzer Avatar asked Feb 26 '23 04:02

acfoltzer


1 Answers

Sounds like you have a good explanation why if is doing a little more than and and or. But if you could cheat and add a lambda to delay the actual result:

(define-syntax-rule (if c t e) ((or (and c (lambda () t)) (lambda () e))))
like image 116
Eli Barzilay Avatar answered Apr 30 '23 08:04

Eli Barzilay