all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Drew Adams" <drew.adams@oracle.com>
Subject: RE: What's wrong with this lisp code in my init file?!
Date: Sun, 31 Dec 2006 14:29:17 -0800	[thread overview]
Message-ID: <DNEMKBNJBGPAOPIJOOICGEEFDKAA.drew.adams@oracle.com> (raw)
In-Reply-To: <87r6ufwx7m.fsf@offby1.atm01.sea.blarg.net>

> But there's an even more general way to do more than one thing inside
> an "if": the "progn" form.  It simply does a bunch of things one after
> the other:
>
>     (if (furryp critter)
>
>         ;; yup, furry
>         (progn
>           (pet critter)
>           (comb-fur critter))
>
>       ;; no fur
>       (progn
>         (apply 'sunscreen critter)
>         (search-for-tattoos critter)))

(The second `progn' is not needed, there, BTW. `if' has an implicit `progn'
for the false branch. I know that Eric knows that, but I thought I'd point
it out.)

While we're on the subject of conditionals and `progn', and hoping not to
open too big a can of worms...

`cond' is very general. Each of its clauses has in implicit `progn'. In this
case, the code would be:

(cond ((furryp critter)
       (pet critter)
       (comb-fur critter))
      (t
       (apply 'sunscreen critter)
       (search-for-tattoos critter)))

However, if there is only one test, and the conditional clause is used only
for effect, not also for the value it returns, then many people (myself
included) prefer to use `when' or `unless', instead of `if' or `cond'. This
is largely a convention for human readers, letting them know that the value
is unimportant. Like `cond' and `if' (second branch), `when' and `unless'
have an implicit `progn'.

In the OP case, the code would be:

(when (eq window-system 'w32)
  (setq ps-printer-name t)
  (setq ps-lpr-command "..."))

Of course, as Eric pointed out, in this case, you can combine the setq's:

(when (eq window-system 'w32)
  (setq ps-printer-name t
        ps-lpr-command "..."))

And there are also `and' and `or', which are sometimes used for nested
conditionals of a particular form, and where the value matters. For example,
this:

(if a
    a
  (if b
      b
    (if c
        c
      nil)))

is often written like this: (or a b c). And this:

(if a
    (if b
        (if c
            c
          nil)
      nil)
  nil)

is often written like this: (and a b c).

If the value is unimportant, some people will combine `when' or `unless'
with `and' or `or'. That is, instead of this:

(and a b c (do-it-to-it))

Some people will write this:

(when (and a b c) (do-it-to-it))

Most of choosing a particular conditional to use is about communicating
intention to (human) readers of your code. Among various choices that do
essentially the same thing, some are sometimes more readable than others or
more clearly indicate what is important.

  reply	other threads:[~2006-12-31 22:29 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-31 16:15 What's wrong with this lisp code in my init file?! Endless Story
2006-12-31 18:48 ` Eric Hanchrow
     [not found] ` <mailman.2592.1167591202.2155.help-gnu-emacs@gnu.org>
2006-12-31 19:57   ` Endless Story
2006-12-31 21:51     ` Eric Hanchrow
2006-12-31 22:29       ` Drew Adams [this message]
     [not found]     ` <mailman.2595.1167601997.2155.help-gnu-emacs@gnu.org>
2006-12-31 22:16       ` Harald Hanche-Olsen
2006-12-31 22:50         ` Drew Adams
     [not found]         ` <mailman.2599.1167605423.2155.help-gnu-emacs@gnu.org>
2007-01-01  1:48           ` Endless Story

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=DNEMKBNJBGPAOPIJOOICGEEFDKAA.drew.adams@oracle.com \
    --to=drew.adams@oracle.com \
    /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.