gpt4 book ai didi

lisp - 如何格式化嵌套的多值绑定(bind)?

转载 作者:行者123 更新时间:2023-12-04 12:26:32 26 4
gpt4 key购买 nike

最近,我经常嵌套几个返回多个值的函数。然而,不像 let ,我允许将这些调用优雅地写成一个大语句,我总是以很多缩进结束。

我的 问题 是:具有多个多值函数,例如

(defun return-1-and-2 ()
(values 1 2))

(defun return-3-and-4 ()
(values 3 4))

是否有可能实现与
(multiple-value-bind (one two)
(return-1-and-2)
(multiple-value-bind (three four)
(return-3-and-4)
(list one two three four)))

但写得更简洁 let -方式,即,类似的东西

(multiple-let (((one two) (return-1-and-2))
((three four) (return-3-and-4)))
(list one two three four))

?

最佳答案

库中可能有类似的结构。

请注意,它更类似于 let* ,而不是 let , 因为范围是嵌套的。

可以写一个宏。例如:

(defmacro multiple-value-let* ((&rest bindings) &body body)

"Sets the scope for several ((var-0 ... var-n) form)
binding clauses, using the multiple return values of the form."

(if (null bindings)
`(progn ,@body)
(destructuring-bind (((&rest vars) form) &rest rest-bindings)
bindings
`(multiple-value-bind ,vars
,form
(multiple-value-let* ,rest-bindings
,@body)))))

例子:
CL-USER 33 > (walker:walk-form
'(multiple-value-let* (((one two) (return-1-and-2))
((three four) (return-3-and-4)))
(list one two three four)))
(MULTIPLE-VALUE-BIND (ONE TWO)
(RETURN-1-AND-2)
(MULTIPLE-VALUE-BIND (THREE FOUR)
(RETURN-3-AND-4)
(PROGN (LIST ONE TWO THREE FOUR))))

关于lisp - 如何格式化嵌套的多值绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60020148/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com