home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / scheme / 2829 < prev    next >
Encoding:
Internet Message Format  |  1992-12-25  |  1.7 KB

  1. Path: sparky!uunet!spool.mu.edu!agate!anarres.CS.Berkeley.EDU!bh
  2. From: bh@anarres.CS.Berkeley.EDU (Brian Harvey)
  3. Newsgroups: comp.lang.scheme
  4. Subject: Re: applying or
  5. Date: 25 Dec 1992 23:50:39 GMT
  6. Organization: University of California, Berkeley
  7. Lines: 30
  8. Message-ID: <1hg6ofINN7ev@agate.berkeley.edu>
  9. References: <BztJDI.1J1@malihh.hanse.de> <BztyDu.KDE@cs.psu.edu>
  10. NNTP-Posting-Host: anarres.cs.berkeley.edu
  11.  
  12. One reason AND and OR are special forms is so that you can have one
  13. clause that checks the validity of another, e.g.,
  14.  
  15.     (and (not (zero? num)) (> (/ 1 num) 7))
  16.  
  17. If AND were an ordinary procedure, Scheme would evaluate both argument
  18. expressions before invoking the procedure, so it would try to divide by
  19. zero before noticing that it's supposed to avoid the second clause in
  20. that case.
  21.  
  22.  
  23. Another reason is just efficiency.  If you see an expression like
  24.  
  25.     (or (= 2 (+ 1 1)) (prime? 184893479827823480745907830796137))
  26.  
  27. you can save a lot of time by noticing that the first clause is true.
  28.  
  29.  
  30. Of course, you *could* do this sort of computation even if AND and OR were
  31. ordinary procedures, by avoiding AND and OR and using IF instead in such
  32. cases.  But this way is deemed more aesthetically pleasing.
  33.  
  34.  
  35. I think it would be nice if the standard specified that AND and OR are
  36. *also* ordinary procedures, so they could be mapped over lists and so on.
  37. The standard recommends against, but allows, the use of keywords as
  38. symbols other than in the car of an expression.  I would change this to
  39. allow the overloading but prebind all such symbols to procedures that
  40. do the same as the special form.  (I wouldn't really care except for the
  41. exercise in The Book that expects you to map cons-stream over a matrix.)
  42.