From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: Advice on writing predicates
Date: Fri, 26 Jun 2009 11:13:38 +0200 [thread overview]
Message-ID: <7cprcrl5ml.fsf@pbourguignon.anevia.com> (raw)
In-Reply-To: uhby454qg.fsf@raytheon.com
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__
next prev parent reply other threads:[~2009-06-26 9:13 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-06-25 22:27 Advice on writing predicates Sarir Khamsi
2009-06-25 23:23 ` Anselm Helbig
2009-06-29 19:18 ` chickenphat
2009-06-26 6:40 ` Daniel Pittman
2009-06-26 7:37 ` Andreas Röhler
2009-06-26 9:13 ` Pascal J. Bourguignon [this message]
2009-06-26 11:02 ` Thien-Thi Nguyen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7cprcrl5ml.fsf@pbourguignon.anevia.com \
--to=pjb@informatimago.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.