Lisp中的'和#'有什么区别? [英] What's the difference between ' and #' in Lisp?

查看:85
本文介绍了Lisp中的'和#'有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎两者

(mapcar 'car '((foo bar) (foo1 bar1))) 

(mapcar #'car '((foo bar) (foo1 bar1)))

工作原理相同.

我也知道'的意思是(引号),而#'的意思是(函数功能名).

And I also know ' means (quote symbol) and #' means (function function-name).

但是潜在的区别是什么?为什么这两个都可以在以前的 mapcar 中使用?

But what's the underlying difference? Why these 2 both work in previous mapcar?

推荐答案

'foo

计算为符号FOO.

#'foo

计算绑定到名称 FOO 的函数.

evaluates to the function bound to the name FOO.

在Lisp中,当符号FOO具有函数绑定时,可以将符号称为函数.CAR是具有功能绑定的符号.

In Lisp a symbol can be called as a function when the symbol FOO has a function binding. Here CAR is a symbol that has a function binding.

但这不起作用:

(flet ((foo (a) (+ a 42)))
  (mapcar 'foo '(1 2 3 4 5)))

这是因为FOO作为符号无法访问本地词法函数,并且当 foo 不是在其他地方定义的函数时,Lisp系统会抱怨.

That's because FOO as a symbol does not access the local lexical function and the Lisp system will complain when foo is not a function defined elsewhere.

我们需要写:

(flet ((foo (a) (+ a 42)))
  (mapcar #'foo '(1 2 3 4 5)))

(函数foo)或其简写符号#'foo表示词法局部函数FOO.

Here the (function foo) or its shorthand notation #'foo refers to the lexical local function FOO.

还请注意

(funcall #'foo ...)

vs.

(funcall 'foo ...)

后一种方法可能还会执行一次间接操作,因为它需要从符号中查找函数,而#'foo直接表示该函数.

The later might do one more indirection, since it needs to lookup the function from the symbol, while #'foo denotes the function directly.

摘要:

如果符号具有功能绑定,则通过符号调用功能即可.

If a symbol has a function binding, calling a function through the symbol works.

这篇关于Lisp中的'和#'有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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