解析Common Lisp列表中的符号 [英] Resolving symbols in a Common Lisp list

查看:102
本文介绍了解析Common Lisp列表中的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个功能

CL-USER> (defun trimmer (seq) "This trims seq and returns a list"
      (cdr 
         (butlast seq)))
TRIMMER
CL-USER> (trimmer '(1 2 3 VAR1 VAR2))
(2 3 VAR1)
CL-USER> 

请注意,由于QUOTE,如何解决VAR1和VAR2的问题.假设我想将符号VAR1和VAR2解析为它们的值-是否有标准功能可以做到这一点?

Notice how, due to QUOTE, VAR1 and VAR2 are not resolved. Suppose I want to resolve the symbols VAR1 and VAR2 to their values - is there a standard function to do this?

推荐答案

请勿使用quote创建带有变量的列表;使用list代替:

Do not use quote to create a list with variables; use list instead:

CL-USER> (trimmer (list 1 2 3 var1 var2))
(2 3 value-of-var1)

(其中value-of-var1var1的值).

Quote仅阻止评估其参数是什么.如果其参数恰好是列表文字,则将其返回.但是,要创建不仅仅是文字列表,请使用list.您可以 使用反引号语法,但这在这种情况下相当令人困惑.

Quote only prevents evaluation of whatever its argument is. If its argument happens to be a list literal, then that is returned. However, to create lists that are not just literals, use list. You can use backquote syntax, but that is rather obfuscation in such a case.

这篇关于解析Common Lisp列表中的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆