Reputation: 2042
(or #t (/ 1 0))
(/ 1 0)
is illegle, but why we do not see an error?
There's an explanation in my lecture note.
The identifier
or
does not refer to a function, but rather a syntactic form that implements short-circuiting.
But I still don't quite understand.
Upvotes: 1
Views: 25
Reputation: 235994
The key part is that or
is a syntactic form, as mentioned in your lecture notes. That means that the evaluation rules are different from a normal procedure, and can be implemented at the interpreter level or (more likely) as a derived expression or a macro. If we tried to implement or
as a procedure, your example would indeed fail:
(define (my-or a b)
(if a a b))
(my-or #t (/ 1 0))
=> /: division by zero
Procedures evaluate all its arguments before passing them to the body . By contrast, or
evaluates its first argument, if it's truthy it returns the value of the first argument, otherwise it returns the result of evaluating the second argument - but it'll never evaluate the second argument if the first one was truthy. This is known as "short circuit" evaluation of boolean connectors, and something similar happens with and
.
To gain a better understanding of what's happening under the hood, please read chapter 4 of SICP, here's the relevant link explaining derived expressions. Quoting: "Some special forms in our language can be defined in terms of expressions involving other special forms, rather than being implemented directly". In this case, or
can be implemented using if
, which in turn is also a syntactic form, implemented as a special case in the interpreter's evaluation procedure.
Upvotes: 1