80

In elisp, there is an 'if' case where I would like to perform many different things:

(if condition
    (do-something)
    (do-another-thing)
    ...)

However, (do-another-thing) is executed in the else-case only. How can you specify a block of instructions to execute? For example:

(if condition
    (begin
        (do-something)
        (do-another-thing)
        ...))
Mustafa Aydın
  • 17,645
  • 4
  • 15
  • 38
Martin Cote
  • 28,864
  • 15
  • 75
  • 99

2 Answers2

107

Use progn:

(if condition
    (progn
        (do-something)
        (do-another-thing)))

See sequencing in the manual.

Mustafa Aydın
  • 17,645
  • 4
  • 15
  • 38
mipadi
  • 398,885
  • 90
  • 523
  • 479
53

If there's no else required, it might be more readable to use:

(when condition
    (do-something)
    (do-another-thing))

And, there's the converse

(unless (not condition)
    (do-something)
    (do-another-thing))

Check out the Emacs Lisp manual for conditionals.

Mustafa Aydın
  • 17,645
  • 4
  • 15
  • 38
Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • 3
    FWIW, I generally follow the convention suggested in *Common Lisp The Language* of using `when` and `unless` when the return value is not important (i.e., they are used for side effects only). I generally use `and` and `or` when the return value is important. I generally use `if` and `cond` when there are multiple branches (whether or not the return value is important). – Drew Oct 29 '13 at 01:07