help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Advice on writing predicates


From: Pascal J. Bourguignon
Subject: Re: Advice on writing predicates
Date: Fri, 26 Jun 2009 11:13:38 +0200
User-agent: Gnus/5.101 (Gnus v5.10.10) Emacs/22.2 (gnu/linux)

Sarir Khamsi <sarir.khamsi@raytheon.com> 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__


reply via email to

[Prev in Thread] Current Thread [Next in Thread]