Q: how can I let
-bind multiple variables conditional on x?
Assume that I have three variables, a
, b
, and c
, that I want
to let
-bind conditional on some other x
. How do I do that
concisely and idiomatically?
Here's an example that works, but is repetitive and error-prone:
(let ((a (if t 1 4))
(b (if t 2 5))
(c (if t 3 6)))
(list a b c)) ; => (1 2 3)
Here's another example that works, but the use of setq
feels
both repetitive and inelegant:
(let (a b c)
(if t
(setq a 1
b 2
c 3)
(setq a 4
b 5
c 6))
(list a b c)) ; => (1 2 3)
Here's an example that doesn't work, because the variables fall
outside the scope of the let
-binding:
(if t
(let ((a 1)
(b 2)
(c 3)))
(let ((a 4)
(b 5)
(c 6)))
(list a b c)) ; => nil
What is the idiomatic way to do this?