jue*_*jue 2 lisp design-patterns common-lisp
假设我要调用function foo,它需要2个参数a和b。(foo a b)。
在某些用例中,这些参数的值相互依赖,如下所示:
(let (a
b)
(if (something-p)
(progn
(setq a 'a1)
(setq b 'b1))
(progn
(setq a 'a2)
(setq b 'b2)))
(foo a b))
Run Code Online (Sandbox Code Playgroud)
是否有(lisp)模式用于此类问题?(还是我需要为此编写自己的宏?)
例子:
应用列表:
(apply #'foo
(if (something-p)
(list 'a1 'b1)
(list 'a2 'b2)))
Run Code Online (Sandbox Code Playgroud)
相似,但具有多个值:
(multiple-value-call #'foo
(if (something)
(values 'a1 'b1)
(values 'a2 'b2)))
Run Code Online (Sandbox Code Playgroud)
绑定多个值:
(multiple-value-bind (a b)
(if (something)
(values 'a1 'b1)
(values 'a2 'b2))
(foo a b))
Run Code Online (Sandbox Code Playgroud)
嵌套let:
(let ((something (something-p)))
(let ((a (if something 'a1 'b2))
(b (if something 'b1 'b2)))
(foo a b)))
Run Code Online (Sandbox Code Playgroud)