0

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

def my_func(): v1; v2; if (some_condition): v1 = 2 v2 = 3 else: v1 = 4 v2 = 5 foo(v1) bar(v2) ... # 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
  • emacs.stackexchange.com/tags/elisp/info Commented Jun 11, 2022 at 0:58
  • I removed the right paren you had after some-condition... Commented Jun 11, 2022 at 1:17

2 Answers 2

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))) 
1

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))) 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.