在Clojure中使用报价 [英] Using quote in Clojure

查看:60
本文介绍了在Clojure中使用报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

引用Clojure会导致无法评估。 ’:a :a 返回相同的结果。 ’:a和:a有什么区别?一个没有被评估,而另一个则对自身进行评估……但这与非评估相同吗?

Quoting in clojure results in non-evaluation. ':a and :a return the same result. What is the difference between ':a and :a ? One is not evaluated and other evaluates to itself... but is this same as non-evaluation ?

推荐答案


  1. ':a (quote:a)的简写。

(eval'(引用形式))根据定义返回形式 。也就是说,如果Clojure函数 eval 接受一个列表结构作为其参数,该列表结构的第一个元素是符号 quote ,它将返回所述列表结构的第二个元素,而不以任何方式对其进行转换(因此,被引用的形式没有被求值)。换句话说,当行为 eval 的参数为(quote foo)就是返回 foo 不变,无论它是什么

(eval '(quote form)) returns form by definition. That is to say, if the Clojure function eval receives as its argument a list structure whose first element is the symbol quote, it returns the second element of said list structure without transforming it in any way (thus it is said that the quoted form is not evaluated). In other words, the behaviour eval dispatches to when its argument is a list structure of the form (quote foo) is that of returning foo unchanged, regardless of what it is.

当您在程序中写下文字:a 时,它将得到读入作为关键字:a ;也就是说,具体的文本:a 被转换为内存数据结构,该结构恰好被称为:a 关键字(Lisp是谐音的,意味着有时很难区分Lisp数据的文本表示形式和数据本身,即使这对于解释目的很有用...)。

When you write down the literal :a in your programme, it gets read in as the keyword :a; that is, the concrete piece of text :a gets converted to an in-memory data structure which happens to be called the :a keyword (Lisp being homoiconic means that occasionally it is hard to distinguish between the textual representation of Lisp data and the data itself, even when this would be useful for explanatory purposes...).

与文字:a 相对应的内存数据结构是一个Java对象,它公开了许多方法等,并且具有一个有趣的属性,函数 eval 在接收到该数据对象作为参数时,将其保持不变。换句话说,您问的关键字的对自身的评估只是 eval 在将关键字作为参数传递时分派给的行为

The in-memory data structure corresponding to the literal :a is a Java object which exposes a number of methods etc. and which has the interesting property that the function eval, when it receives this data object as an argument, returns it unchanged. In other words, the keyword's "evaluation to itself" which you ask about is just the behaviour eval dispatches to when passed in a keyword as an argument.

因此,当 eval 看到':a ,将其视为带引号的形式并返回其第二部分,恰好是:a 。另一方面,当 eval 看到:a 时,它将其视为关键字并返回不变。两种情况下的返回值都相同(只是关键字:a );

Thus when eval sees ':a, it treats it as a quoted form and returns the second part thereof, which happens to be :a. When, on the other hand, eval sees :a, it treats it as a keyword and returns it unchanged. The return value is the same in both cases (it's just the keyword :a); the evaluation process is slightly different.

Clojure语义(实际上是Lisp的任何方言的Lisp语义)都是根据和返回的值指定的函数 eval 在接收各种Lisp数据结构作为参数时引起的副作用。因此,以上内容说明了在程序中写下':a :a 时实际发生的情况(代码像(println:a)可能会编译成有效的字节码,而实际上并没有对函数 eval 进行编码;但语义始终保留下来,因此它仍然像 一样起作用,即 eval 接收包含符号 println 和关键字:a )。

Clojure semantics -- indeed Lisp semantics, for any dialect of Lisp -- are specified in terms of the values returned by and side-effects caused by the function eval when it receives various Lisp data structures as arguments. Thus the above explains what's actually meant to happen when you write down ':a or :a in your programme (code like (println :a) may get compiled into efficient bytecode which doesn't actually code the function eval, of course; but the semantics are always preserved, so that it still acts as if it was eval receiving a list structure containing the symbol println and the keyword :a).

这里的关键思想是,无论所评估的表单是':a 还是:a ,关键字数据在读取时构造结构;那么当评估其中一种形式时,该数据结构将保持不变-尽管出于不同的原因。

The key idea here is that regardless of whether the form being evaluated is ':a or :a, the keyword data structure is constructed at read time; then when one of these forms is evaluated, that data structure is returned unchanged -- although for different reasons.

这篇关于在Clojure中使用报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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