From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: pjb@informatimago.com (Pascal J. Bourguignon) Newsgroups: gmane.emacs.help Subject: Re: Advice on writing predicates Date: Fri, 26 Jun 2009 11:13:38 +0200 Organization: Anevia SAS Message-ID: <7cprcrl5ml.fsf@pbourguignon.anevia.com> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1246013562 29212 80.91.229.12 (26 Jun 2009 10:52:42 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 26 Jun 2009 10:52:42 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Jun 26 12:52:35 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1MK92o-0000kc-Ij for geh-help-gnu-emacs@m.gmane.org; Fri, 26 Jun 2009 12:52:34 +0200 Original-Received: from localhost ([127.0.0.1]:43071 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MK92m-0000iE-Sb for geh-help-gnu-emacs@m.gmane.org; Fri, 26 Jun 2009 06:52:32 -0400 Original-Path: news.stanford.edu!headwall.stanford.edu!news.glorb.com!news2.glorb.com!proxad.net!feeder1-2.proxad.net!cleanfeed3-b.proxad.net!nnrp1-1.free.fr!not-for-mail Original-Newsgroups: gnu.emacs.help Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.101 (Gnus v5.10.10) Emacs/22.2 (gnu/linux) Cancel-Lock: sha1:EMlNdIEHuXiRVEqmX73ytJv0UnY= Original-Lines: 54 Original-NNTP-Posting-Date: 26 Jun 2009 11:13:38 CEST Original-NNTP-Posting-Host: 88.170.236.224 Original-X-Trace: 1246007618 news-2.free.fr 11917 88.170.236.224:35037 Original-X-Complaints-To: abuse@proxad.net Original-Xref: news.stanford.edu gnu.emacs.help:170331 X-Mailman-Approved-At: Fri, 26 Jun 2009 06:47:36 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:65549 Archived-At: Sarir Khamsi writes: > I wrote a simple predicate that returns t if the Emacs major version > number is greater than 22 and would like some advice/comments: > [...] > > (if (> 22 (car version-num)) > t > nil)) > > seems to work. I know that I don't need to save \2 and \3 but wanted > that for a later function. Any comments or suggestions on how to make > this better? Thanks. 1- anything that is not nil is true, so you don't really need to return t, just something not nil. 2- > already returns either t or nil. So you can just write (> 22 emacs-major-version). 3- even if > didn't return t for true, you could just write (> 22 emacs-major-version) (see point 1). An famous example of a function that doesn't return t or nil, but a generalized boolean is member: (member 'b '(a b c)) --> (b c) ; which is true. So you can write either: (defun containp (x l) (member x l)) ; returns a generalized boolean (defun following (x l) (second (member x l))) ; returns the element following x in l. (let ((list '(a b c)) (e 'b)) (when (containp e list) (following e list))) --> c 4- but if you really wanted to return t or nil in case 3, you could write instead of if: (not (not (> 22 emacs-major-verion))). Which could be rewritten as (not (<= 22 emacs-major-verion)). 5- one of my teacher taught us to always use < or <=, never > or >=, so you get to write always the smallest on the left and the biggest on the right, which is more readable, in the long term. In conclusion, you could write it better as: (< emacs-major-version 22) -- __Pascal Bourguignon__