可以安全地忽略宏和内置函数之间的区别吗? [英] Can one safely ignore the difference between a macro and a built-in?

查看:151
本文介绍了可以安全地忽略宏和内置函数之间的区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Clojure开始,这也是我的第一个lisp。显然有很多东西需要接受,为了减轻认知负荷,我尝试找到我可以放心忽略的部分(现在)。

I'm starting out with Clojure, which is also my first lisp. There's obviously a lot to take in, and in an attempt to lessen the cognitive load, I try to find the parts which I can safely ignore (for now).

一个安全地处理具有宏的表单和内置的表单相同,或有陷阱会后来弹出?

Can one safely treat forms with macros and forms with built-ins the same, or are there pitfalls that will spring up later?

换句话说,我会遇到一个情况,我需要知道(defn f1 [])扩展为

In other words, will I ever run into a situation where I need to know that (defn f1 []) expands to

(def f1 (.withMeta (clojure.core/fn f1 ([])) (.meta (var f1))))


推荐答案

宏在一般情况下是非常不同的。宏不是头等公民:你不能将它们传递给 map 等,你不能将它们存储在变量中,你不能应用到参数列表。最初,你不需要担心这些,因为他们会显然不工作:一个微妙的,难以察觉的错误将是一个更麻烦。如果尝试运行

Macros in general compose very differently. Macros are not "first class citizens": you can't pass them to map etc., you can't store them in variables and you can't apply them to a list of arguments. Initially you don't need to worry about these because they will obviously not work: a subtle, hard-to-detect error would be a lot more troubling. If you try to run

(map if [true false true false] [1 2 3 4] [-1 -2 -3 -4])

这是事实,如果不是函数变得非常明显。只要记住,宏不是函数,你应该很好:)

the fact that if is not a function will become very apparent. Just keep in mind that macros are not functions and you should be just fine :)

PS:宏是(fn(fn D):^ P):O)

这篇关于可以安全地忽略宏和内置函数之间的区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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