Search code examples
lispelisp

how to set a bunch of variables depending on one condition in elisp?


Suppose I want to set the values of bar and baz depending on one condition, which is the same in both cases, say the value of foo. Using the let special form, I do something like this

(let ((bar (if foo 1 2))
      (baz (if foo 3 4)))
  ... )

While the above program is correct, it seems a little strange as it checks the value of foo twice. Is there an idiomatic expression one can use in such cases to avoid the double-check?


Solution

  • You needn't set the values in the let form itself. The let form creates the local bindings, after which you can set values however you so wish.

    (let (bar baz)
      (if foo
          (setq bar 1
                baz 2)
        (setq bar 3
              baz 4))
      ...)