This is the procedural logic I want to program in Elisp:

def my_func():

    if (some_condition):
        v1 = 2
        v2 = 3
        v1 = 4
        v2 = 5

    ... # Possibly many other lines of code.

I have this in Elisp:

(defun my_func ()
  (if some_condition
      (let ((v1 2)
            (v2 3))
        (foo v1)
        (bar v2))
    (let ((v1 4)
          (v2 4))
      (foo v1) ; This block of code is duplicated and could be very long.
      (bar v2))))

What is the Llisp way of programming the logic shown above? That is, I have a few variables at the start of my function that can take some values depending on some condition, then the rest of the function goes on.


2 Answers 2


For the sake of providing a more basic version of the answer by @db48x you could do the same thing like this:

(defun my-func ()
  (let (v1 v2)
    (if some-condition
        (setq v1 2 v2 3)
      (setq v1 4 v2 5))
    (foo v1)
    (bar v2)))

There are as many ways of writing this as there are people, but here is how I might do it:

(defun my-func ()
  (cl-multiple-value-bind (v1 v2)
      (if some-condition
          (cl-values 2 3)
        (cl-values 4 5))
    (foo v1)
    (bar v2)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.