home *** CD-ROM | disk | FTP | other *** search
- 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
- From: zaidel@muzungu.cis.upenn.edu (Martin J. Zaidel)
- Newsgroups: comp.lang.lisp
- Subject: Elegant control structure wanted
- Message-ID: <98373@netnews.upenn.edu>
- Date: 19 Nov 92 16:31:05 GMT
- Sender: news@netnews.upenn.edu
- Organization: University of Pennsylvania
- Lines: 43
- Nntp-Posting-Host: muzungu.cis.upenn.edu
-
- I've written an equality predicate to test two structures. My
- predicate is of the form:
-
- (defun node-equal (node1 node2)
- (and (slot1-equal slot1 slot2)
- (slot2-equal slot1 slot2)
- (slot3-equal slot1 slot2)))
-
- (Actually, the two node arguments have vastly different structures, but
- that's not the issue here.)
-
- I wanted to add format statements so that when NODE-EQUAL returned nil,
- I'd know which slot caused the failure. So I tried UNLESS:
-
- (defun node-equal (node1 node2)
- (and (unless (slot1-equal slot1 slot2)
- (format t "Slot 1 not equal"))
- (unless (slot2-equal slot1 slot2)
- (format t "Slot 2 not equal"))
- (unless (slot3-equal slot1 slot2)
- (format t "Slot 3 not equal"))))
-
- This won't work, since UNLESS will return NIL, regardless of the
- value of the slot-equal test. Now, CLtL2 p.158 advises that, when the
- value of the unless is relevant (as it is here), to use IF instead.
-
- That means my code is:
-
- (defun node-equal (node1 node2)
- (and (if (slot1-equal slot1 slot2)
- t
- (format t "Slot 1 not equal"))
- (if (slot2-equal slot1 slot2)
- t
- (format t "Slot 2 not equal"))
- (if (slot3-equal slot1 slot2)
- t
- (format t "Slot 3 not equal"))))
-
- This explicit use of T for the value of the IF strikes me as rather
- inelegant. I'd appreciate suggestions for a nicer solution.
-
- Thanks in advance.
-