2

For this simple question, I never found any answer. Let assume I have the following:

(if (true)
    (foo1) ;; When true
    (bar) ;; When false)

So I would like to combine foo1 and foo2

(if (true)
    (foo1)(foo2) ;; When true
    (bar) ;; When false)

The problem is that the Emacs Lisp interpreter consider foo2 as false statement. Trying to devise some workaround, I could get away with the following:

(if (true)
    (foo3) ;; When true
    (bar) ;; When false)

(defun foo3 ()
    (foo1)
    (foo2))

But this could be quite cumbersome when you use if-else statements a lot. How could I find a more practical way for this?

Drew
  • 75,699
  • 9
  • 109
  • 225
ReneFroger
  • 3,855
  • 22
  • 63

1 Answers1

5

Use progn or prog1. progn takes a list of body forms, evaluates them one by one, then returns the value of the last one. prog1 does the same but returns the value of the first one.

(if (true)
  (progn (foo1)
         (foo2))
  (bar))
db48x
  • 15,741
  • 1
  • 19
  • 23
  • Thanks for your answer! Emacs Lisp is so flexible, which makes it difficult to me to keep sight on all available options. – ReneFroger Nov 23 '15 at 19:51