Question

I am currently studying scheme and came across the following question:

prove or disprove the following statement: the procedure my-or:

     (define my-or
       (lambda (test1 test2)
            (if test1
                #t
                test2)))

works the same as the built-in procedure "or" with two arguments. tipp:

(or) = #f

(or <a1> ... <an>) = (if <a1>
                          #t
                         (or <a2> ... <an>))

if you disprove the statement, find a program where the difference between the two procedures can be noticed.

so, I believe there is no difference between the two procedures, since they both work the same way for two arguments (portrayed by this tree-diagram that is based on the racket-documentation of the procedure if and or):

                       if/or
                    ..     ..
                 #f          #t
               ..              ..
            test2                outcome: #t
          ..     ..
        #f         #t
      ..             ..
   outcome: #f        outcome: #t

and also, because if I rewrite my-or in the way it is mentioned in the second tipp I still get the same output, for example:

     > (my-or (= 10 10) (> 2 5)) 
       --> #t
     > (my-or (> 23 42) (< 5 2)) 
       --> #t

rewritten:

     > (if (= 10 10) #t (or (> 2 5)))
       --> #t
     > (if (> 23 42) #t (or (< 5 2)))
       --> #f

the only thing that bothers me is the feeling that we are supposed to disprove the statement due to the mentioning of: "if you disprove the statement, find a programm where the difference between the two procedures can be noticed."

This is why I am not wondering if somebody has an idea to disprove the statement or if somebody can otherwise confirm my idea how to prove the statement to be right..

Was it helpful?

Solution

In fact, the procedure-based version my-or is different from the built-in or. To prove it, just try this:

(or 1 (/ 1 0))
=> 1

(my-or 1 (/ 1 0))
=> /: division by zero

Explanation:

  • The built-in or is a special form, meaning: the evaluation rules that normally apply for procedures are different for or (and by the way, the same is true for and)
  • or short-circuits the evaluation of its parameters: the value of the first parameter that returns non-false is returned as the result, and the rest of the parameters will not be evaluated!
  • my-or performs a normal function call, and all its parameters will be evaluated before passing them to my-or's body
  • The example above shows that clearly: or doesn't evaluate the division by zero, it returns as soon as it finds the first non-false value, whereas my-or evaluates both parameters before proceeding, and it fails because it encounters a division by zero
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top