home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / lisp / 2920 < prev    next >
Encoding:
Text File  |  1992-11-19  |  2.3 KB  |  66 lines

  1. Path: sparky!uunet!think.com!barmar
  2. From: barmar@think.com (Barry Margolin)
  3. Newsgroups: comp.lang.lisp
  4. Subject: Re: Elegant control structure wanted
  5. Date: 19 Nov 1992 21:33:15 GMT
  6. Organization: Thinking Machines Corporation, Cambridge MA, USA
  7. Lines: 54
  8. Message-ID: <1eh16rINNl8i@early-bird.think.com>
  9. References: <98373@netnews.upenn.edu>
  10. NNTP-Posting-Host: telecaster.think.com
  11.  
  12. In article <98373@netnews.upenn.edu> zaidel@muzungu.cis.upenn.edu (Martin J. Zaidel) writes:
  13. >(defun node-equal (node1 node2)
  14. >   (and (unless (slot1-equal slot1 slot2)
  15. >        (format t "Slot 1 not equal"))
  16. >     (unless (slot2-equal slot1 slot2)
  17. >        (format t "Slot 2 not equal"))
  18. >     (unless (slot3-equal slot1 slot2)
  19. >        (format t "Slot 3 not equal"))))
  20. >
  21. >This won't work, since UNLESS will return NIL, regardless of the 
  22. >value of the slot-equal test.  
  23.  
  24. You can use OR instead of UNLESS; in fact, this is what people did all the
  25. time before UNLESS was introduced.  The code then becomes
  26.  
  27. (defun node-equal (node1 node2)
  28.   (and (or (slot1-equal node1 node2)
  29.        (format t "~&Slot 1 not equal.~%"))
  30.        (or (slot2-equal node1 node2)
  31.        (format t "~&Slot 1 not equal.~%"))
  32.        (or (slot3-equal node1 node2)
  33.        (format t "~&Slot 1 not equal.~%"))))
  34.  
  35. Actually, I prefer not to depend on the return value of FORMAT being NIL
  36. (because I don't remember whether such return values are guaranteed), so
  37. I'd probably write something like:
  38.  
  39. (defun node-equal (node1 node2)
  40.   (and (or (slot1-equal node1 node2)
  41.        (progn (format t "~&Slot 1 not equal.~%")
  42.                   nil))
  43.        (or (slot2-equal node1 node2)
  44.        (progn (format t "~&Slot 1 not equal.~%")
  45.                   nil))
  46.        (or (slot3-equal node1 node2)
  47.        (progn (format t "~&Slot 1 not equal.~%")
  48.                   nil))))
  49.  
  50. And now that the code is starting to look messy and repetitive, I'd
  51. probably turn the repetitive code into a local macro:
  52.  
  53. (defun node-equal (node1 node2)
  54.   (macrolet ((compare (tester slot-number)
  55.                `(or (,tester node1 node2)
  56.                     (progn (format t "~&Slot ~D not equal.~%" ,slot-number)
  57.                            nil))))
  58.     (and (compare slot1-equal 1)
  59.          (compare slot2-equal 2)
  60.          (compare slot3-equal 3))))
  61. -- 
  62. Barry Margolin
  63. System Manager, Thinking Machines Corp.
  64.  
  65. barmar@think.com          {uunet,harvard}!think!barmar
  66.