函数指示符不受欢迎?

发布于 2024-12-08 23:00:27 字数 606 浏览 0 评论 0原文

浏览 Common Lisp 源代码,我注意到人们最常使用 #'foo,其中 'foo 就足够了——也就是说,只要函数指示符被接受,他们就会选择传递一个功能。

当然,当 foo 通过 flet 等定义时,#'foo 是必需的。我了解这一切的机制——我的问题是风格之一。难道只是因为人们不想考虑 'foo#'foo,所以他们使用后者,因为前者有时不起作用?即使是这样,也无法解释 #'(lambda ...) 的使用,因为 #' 在那里总是是不必要的。

CL 有时因为 #' 被认为丑陋,而且大多数新手没有意识到(我敢说)在大多数情况下它是不必要的。我不是新人,但我碰巧更喜欢 'foo。为什么我不寻常?如果我发布一些为 funcallapply 提供符号的代码,我会被嘲笑和羞辱吗?我正在考虑在我的领域开设一个函数指定者匿名分会。我怀疑人们想要使用功能指示符,但由于同行压力,不敢“公开”这一点。

Browsing through Common Lisp sources I notice that people most often use #'foo where 'foo would suffice – that is, wherever a function designator is accepted, they choose to pass a function.

Of course #'foo is necessary when foo is defined via flet and so forth. I understand the mechanics of it all – my question is one of style. Is it just because people don't want to think about 'foo versus #'foo, so they use the latter because the former will sometimes not work? Even if that were so, it wouldn't explain the use of #'(lambda ...) because #' is always unnecessary there.

CL is sometimes called ugly because of #', and most newcomers don't realize that it's unnecessary in (I daresay) the majority of cases. I'm not a newcomer but I happen to prefer 'foo. Why am I unusual? If I publish some code that gives symbols to funcall and apply, will I be mocked and humiliated? I am considering starting a Function Designators Anonymous chapter in my area. I suspect that people want to use function designators but, due to peer pressure, are afraid to "come out" about it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帥小哥 2024-12-15 23:00:27

使用 #' 在概念上更简单:无论您处理的是匿名函数、通过调用 compile 获得的函数,还是使用 #',你总是引用一个函数对象。鉴于此,将符号传递给 mapfuncall 是一种奇怪的特殊情况,它根本不像传递函数对象那么直观。

然而,在某些情况下,符号在概念上可能更合适,例如 make-hash-table:test 参数。在本例中,您将从键比较函数的名称指定的四种不同类型的哈希表中选择一种。在这种情况下,我更喜欢使用符号,因为使用函数对象来区分一种哈希表和另一种哈希表是没有意义的。 (这也是一种误导,因为它往往会欺骗您,让您相信您可以将任意等价谓词传递给 make-hash-table。)

Using #' is conceptually simpler: Whether you're dealing with an anonymous function, a function obtained by calling compile, or a function referenced with #', you're always referencing a function object. Given this, passing a symbol to map or funcall is an odd special case that is simply not as intuitive as passing a function object.

However, there are cases where symbols are arguably conceptually more appropriate, such as in the case of the :test argument to make-hash-table. In this case, you're selecting one out of four different kinds of hash tables specified by the name of the key comparison function. I prefer a symbol in this case, since there is no point in using a function object to distinguish one kind of hash table from another. (It is also misleading, since it tends to deceive you into believing that you can pass just any arbitrary equivalence predicate to make-hash-table.)

那片花海 2024-12-15 23:00:27

它们不一样。 'foo 是对 foo 函数的全局定义的引用,并且是旧时代作用域非常混乱的遗留物。

CL-USER(1): (defun foo (x) 1)
FOO
CL-USER(2): (flet ((foo (x) 2)) (mapcar #'foo '(1 2 3)))
(2 2 2)
CL-USER(3): (flet ((foo (x) 2)) (mapcar 'foo '(1 2 3)))
(1 1 1)

They are not the same. 'foo is a reference to whatever happens to be the global definition of a foo function, and is a leftover from old times when scopes where muchly confused.

CL-USER(1): (defun foo (x) 1)
FOO
CL-USER(2): (flet ((foo (x) 2)) (mapcar #'foo '(1 2 3)))
(2 2 2)
CL-USER(3): (flet ((foo (x) 2)) (mapcar 'foo '(1 2 3)))
(1 1 1)
锦欢 2024-12-15 23:00:27

这是两个不同的风格决定,任何人都必须自己做出。我从未见过对这四种组合的任何批评。

就我个人而言,我更喜欢使用 #' 而不是 ',因为它使函数更加可见。我认为,这一点也不难看——相反,我喜欢这种更明确的语法。不过,我很少会错过使用 Clojure 进行编程的情况。

但我使用 lambda 时没有使用尖引号。对此的详细讨论可以在 Let over Lambda 中找到。我相信,#'(lambda... 的原始论点是 Kent Pitman 提出的。

Those are 2 separate style decisions, that anyone has to make for himself. I've never seen any criticism of any of the four combinations.

Personally I prefer to use #' instead of ', because it makes functions more visible. I think, this isn't ugly at all — on the contrary, I like this more explicit syntax. Although, programming in Clojure, I only rarely miss it.

Yet I use lambda without sharp-quote. A good discussion of this can be found in Let over Lambda. And the original argument for #'(lambda... goes to Kent Pitman, I believe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文