宏,clojure vs普通lisp [英] Macros, clojure vs common lisp

查看:144
本文介绍了宏,clojure vs普通lisp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的几个朋友和我正在一个新的平台上工作,我们想在lisp中构建它。主要景点是宏。我们都使用Common Lisp,但我想探索Clojure的选项。

A few of my friends and I are working on a new platform and we want to build it in lisp. The main attraction are macros. We all use Common Lisp but I want to explore the option of Clojure.

当我提出这一个时,他们说宏系统是弱。我想知道这是真的,在什么地方。

When I proposed this one of them said that the macro system is 'weaker'. I wanted to know if this is true, and in what areas.

推荐答案


  • 它们在编译时执行

  • 可以执行任意变换和生成代码(利用Lisp代码的同质性

  • 它们是合适的使用新的语言结构或DSL来扩展语言

  • 您感觉非常强大,并且效率非常高(在平均方式)

  • They execute at compile time
  • They can perform arbitrary transformation and generation of code (exploiting the homoiconicity of Lisp code)
  • They are suitable for "extending the language" with new language constructs or DSLs
  • You feel very powerful, and can be very productive (in the beating the averages way)

现在有些差异:

Common Lisp还支持阅读器宏,允许您更改阅读器的行为。这允许您引入全新的语法(例如,对于数据结构文字)。这可能是你的朋友把Clojure的宏系统描述为较弱的原因,因为Clojure不允许读取宏。在Clojure你基本上停留在语法(宏名称....)但除了,你可以做任何你想要的。意见分为是否读者宏是一件好事:我个人的看法不是,因为它不给你任何额外的力量,并有潜在的导致极度混乱。

Common Lisp also allows reader macros which allow you to change the behaviour of the reader. This allows you to introduce completely new syntax (e.g. for data structure literals). This might be the reason that your friend describes Clojure's macro system as "weaker" since Clojure does not allow reader macros. In Clojure you are basically stuck with the syntax (macro-name ....) but apart from that you can do anything you want. Opinion is divided as to whether reader macros a good thing or not: my personal view is not, as it doesn't give you any extra "power" and has the potential to cause extreme confusion.

在我看来,Clojure是一个更好的命名空间实现,我认为Clojure的宏系统更容易使用。 Clojure中的每个符号都是命名空间限定的,因此不同的库可以在自己的命名空间中定义相同的符号。因此 + 可以分别定义为 clojure.core / + 和my.vector.library / + ,没有任何冲突的风险。在您自己的命名空间中,您可以从另一个命名空间使用定义,这意味着您可以选择使用<$来自 clojure.core my.vector.library 的c $ c> + 需要。

Clojure has, in my view, a nicer implementation of namespaces that I think make Clojure's macro system easier to use. Every symbol in Clojure is namespace-qualified, so different libraries can define the same symbol different in their own namespace. So + can be defined separately as clojure.core/+ and my.vector.library/+ without any risk of conflicts. In your own namespace, you can use definitions from another namespace, which will mean that you can choose to take + from either clojure.core or my.vector.library as needed.

另一方面,Clojure对于地图 {} 具有额外字面值 code> [] 。这些比传统的Lisp s表达式给你更多的表达性(在简洁的可读语法的意义上)。特别的,使用[]绑定表单是一个约定在Clojure,我认为工作很好的宏和正常的代码 - 它使它们从其他括号中清楚地显示。

Clojure on the other hand has extra literals for maps {} and vectors []. These give you a bit more expressivity (in the sense of concise readable syntax) than traditional Lisp s-expressions. In particular, the use of [] for binding forms is a convention in Clojure that I think works well both for macros and normal code - it makes them stand out clearly from the other parentheses.

Clojure也是一个 Lisp-1 (像Scheme),所以它没有一个单独的函数和数据的命名空间。 Common Lisp是一个 Lisp-2 ,它具有单独的函数和数据名称(因此您可以同时拥有一个名为foo的函数和一个名为foo的数据项)。我稍微偏好Lisp-1方法,因为它更简单,当你在功能语言中写代码和数据之间的划分似乎有点任意。这可能是一个个人的味道。

Clojure is also a Lisp-1 (like Scheme) so it doesn't have a separate namespace for functions and data. Common Lisp is a Lisp-2 which has separate function and data namesapces (so you can have both a function called foo and a data item called foo). I slightly prefer the Lisp-1 approach, since it is simpler and the division between code and data seems a bit arbitrary when you are writing in a functional langauge. This is probably a personal taste thing though.

总的来说,差异相对较小。我认为Clojure是一个更简单,更优雅,而Common Lisp有一些额外的功能(使用自己的风险!)。两者都非常有能力,所以你不能错过选择。

Overall, the differences are relatively minor. I think Clojure is a bit simpler and more elegant, whereas Common Lisp has some extra features (use at your own risk!). Both are extremely capable, so you can't go wrong choosing either.

这篇关于宏,clojure vs普通lisp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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