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

  1. Path: sparky!uunet!ornl!rsg1.er.usgs.gov!darwin.sura.net!zaphod.mps.ohio-state.edu!ub!dsinc!netnews.upenn.edu!muzungu.cis.upenn.edu!zaidel
  2. From: zaidel@muzungu.cis.upenn.edu (Martin J. Zaidel)
  3. Newsgroups: comp.lang.lisp
  4. Subject: Elegant control structure wanted
  5. Message-ID: <98373@netnews.upenn.edu>
  6. Date: 19 Nov 92 16:31:05 GMT
  7. Sender: news@netnews.upenn.edu
  8. Organization: University of Pennsylvania
  9. Lines: 43
  10. Nntp-Posting-Host: muzungu.cis.upenn.edu
  11.  
  12. I've written an equality predicate to test two structures.  My
  13. predicate is of the form:
  14.  
  15. (defun node-equal (node1 node2)
  16.    (and (slot1-equal slot1 slot2)
  17.     (slot2-equal slot1 slot2)
  18.     (slot3-equal slot1 slot2)))
  19.  
  20. (Actually, the two node arguments have vastly different structures, but 
  21. that's not the issue here.)
  22.  
  23. I wanted to add format statements so that when NODE-EQUAL returned nil,
  24. I'd know which slot caused the failure.  So I tried UNLESS:
  25.  
  26. (defun node-equal (node1 node2)
  27.    (and (unless (slot1-equal slot1 slot2)
  28.        (format t "Slot 1 not equal"))
  29.     (unless (slot2-equal slot1 slot2)
  30.        (format t "Slot 2 not equal"))
  31.     (unless (slot3-equal slot1 slot2)
  32.        (format t "Slot 3 not equal"))))
  33.  
  34. This won't work, since UNLESS will return NIL, regardless of the 
  35. value of the slot-equal test.  Now, CLtL2 p.158 advises that, when the 
  36. value of the unless is relevant (as it is here), to use IF instead.
  37.  
  38. That means my code is:
  39.  
  40. (defun node-equal (node1 node2)
  41.    (and (if (slot1-equal slot1 slot2) 
  42.         t
  43.         (format t "Slot 1 not equal"))
  44.     (if (slot2-equal slot1 slot2)
  45.         t
  46.         (format t "Slot 2 not equal"))
  47.     (if (slot3-equal slot1 slot2)
  48.         t
  49.         (format t "Slot 3 not equal"))))
  50.  
  51. This explicit use of T for the value of the IF strikes me as rather
  52. inelegant.  I'd appreciate suggestions for a nicer solution.
  53.  
  54. Thanks in advance.
  55.