Why can't I define "or-function" in the schema?

I found this question about the special function "or" in the circuit:

Joe Hacker loudly declares that there is no reason, or you need to be special in the Scheme - you can simply define it as a programmer, for example:

(define (or xy) (if x #t y)) 

Is Joe right?

I cannot understand why this should not be possible.

Could some expert example explain if this works, and if not: why not?

+6
source share
2 answers

This is because this version of or evaluates all of its arguments (since it is a function), and the standard scheme is or (which is not a function, but a special syntax). Try running (or #t (exit)) on the REPL schema, and then try to do the same with your or function.

The behavior of the standard or sometimes called short-circuited : it evaluates only those arguments that it needs. This is very common for the binary logical operator ( or and and ) in all programming languages. The fact that or looks like a function call is a sign of Scheme / Lisp syntax, but it looks cheated.

+13
source

Whether this will work or not depends on what you want. This certainly works in the sense that for two given Boolean values, it will return the expected result. However, it will not be functionally equivalent to regular or , because it is not short-circuited, i.e. If your definition (or #t (/ 0 0)) throws an error because you divide 0 by 0 using regular or , it will simply return #t and do not try to evaluate (/ 0 0) .

+1
source

Source: https://habr.com/ru/post/900844/


All Articles