Arc Forumnew | comments | leaders | submitlogin
Consistency of check
1 point by silence 5741 days ago | 3 comments
The macro check is defined as:

  check expr test [alt]
What if it were defined like this:

  check test expr [alt]
From my noob perspective, it seems clearer to have the test come first so that it is consistent with if, when, unless etc.


4 points by skenney26 5741 days ago | link

In the definition of check a uniq is assigned the value of expr to avoid multiple evaluation. Therefore, expr is evaluated before test. It's a good idea to order arguments in the same order they are evaluated.

Section 10.2 (page 135) in On Lisp (http://paulgraham.com/onlisp.html) discusses why order of evaluation can sometimes be an issue.

-----

3 points by silence 5740 days ago | link

Thanks for that reference.

Check is currently defined like this:

  (mac check (x test (o alt))
    (w/uniq gx
      `(let ,gx ,x
         (if (,test ,gx) ,gx ,alt))))
Can the order of evaluation problem be corrected by changing the check macro to the following?

  (mac check (test x (o alt))
    (w/uniq gx
      `(let ,gx nil
          (if (,test (= ,gx ,x)) ,gx ,alt))))

-----

3 points by skenney26 5740 days ago | link

Interesting twist. I don't immediately see any issues with doing it that way (other than making the definition slightly longer).

-----