gpt4 book ai didi

lisp - 在解构绑定(bind)中声明未使用变量的任何好方法?

转载 作者:太空宇宙 更新时间:2023-11-03 18:34:42 25 4
gpt4 key购买 nike

我想不通,有没有什么办法可以在 erlang 中为解构绑定(bind)中的“未使用的值”添加类似 _ 的内容?

例如我们有这样的东西:

(destructuring-bind ((_SNIPPET
(_TITLE . title)
(_DESCRIPTION . description)
_RESOURCE-ID (_VIDEO-ID . video-id)))) entry
(declare (ignore
_SNIPPET _TITLE _DESCRIPTION _RESOURCE-ID _VIDEO-ID))
(list video-id title description)))

最好不要为每个未使用的值放置特定的变量,而是编写类似这样的内容:

 (destructuring-bind ((_
(_ . title)
(_ . description)
(_ (_ . video-id)))) entry
(list video-id title description)))

有没有办法通过标准解构绑定(bind)或任何其他标准宏来获得这种行为?或者我必须使用一些类似 ML 的模式匹配库,如果是的话 - 哪个?

最佳答案

DESTRUCTURING-BIND 是不可能的(你不能多次使用一个变量,有些编译器会报错)。您可以枚举变量,_1_2,...但是您必须忽略它们中的每一个。

LOOP 可以做到:

CL-USER 23 > (loop for ((a b nil c) nil d) in '(((1 2 3 4) 5 6)
((1 2 3 4) 5 6))
collect (list a b c d))
((1 2 4 6) (1 2 4 6))

NIL 用作通配符变量。

您可以重用 LOOP 宏:

(defmacro match-bind (pattern object &body body)
`(loop with ,pattern = ,object
while nil
finally (return (progn ,@body))))

CL-USER 37 > (match-bind ((a b nil c) nil d)
'((1 2 3 4) 5 6)
(list a b c d))
(1 2 4 6)

您可以使用一些库中的LET-MAT​​CH。例如:https://github.com/schani/clickr/blob/master/let-match.lisp可能还有更多花哨的版本。

关于lisp - 在解构绑定(bind)中声明未使用变量的任何好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25463763/

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