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

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

RE: What's wrong with this lisp code in my init file?!


From: Drew Adams
Subject: RE: What's wrong with this lisp code in my init file?!
Date: Sun, 31 Dec 2006 14:29:17 -0800

> 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 via email to

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