简体   繁体   English

什么是“空”,“程序”? 和“符号?” 在下面的代码中意味着什么?

[英]What does 'null?', 'procedure?' and 'symbol?' mean in the following code?

I am going through the following documentation for implementing a Lisp Interpreter in Python: http://norvig.com/lispy.html 我正在阅读以下文档,以在Python中实现Lisp解释器: http : //norvig.com/lispy.html

In the standard_env function, a dictionary has been defined for mapping certain symbols or variables to their corresponding functions or values. standard_env函数中,已定义了一个字典,用于将某些符号或变量映射到其相应的函数或值。 However, I am not being able to understand the following entries in the env dictionary: 但是,我无法理解env词典中的以下条目:

env = {}
env.update({
    'equal?':  op.eq, 
    'list?':   lambda x: isinstance(x,list),
    'null?':   lambda x: x == [], 
    'number?': lambda x: isinstance(x, (int, float)),   
    'procedure?': callable,
    'symbol?': lambda x: isinstance(x, str),
})

What does the '?' 什么是“?” at the end of the key mean? 最后的关键是什么意思? Could you please provide some examples where these entries will be used? 您能否提供一些使用这些条目的示例?

Syntactically ? 语法上? is just one of the many characters allowed in identifiers in Lisp. 只是Lisp标识符中允许的许多字符之一。 So it's simply part of the function name. 因此,它只是函数名称的一部分。

By convention function names ending in ? 按照约定,函数名称以?结尾? are used for functions that return boolean values. 用于返回布尔值的函数。

Specifically equal? 具体equal? checks whether its two arguments are equal, null? 检查其两个参数是否相等,是否为null? checks whether its argument is an empty list and all the other ones are type checks, ie they check whether their argument is of the given type. 检查其参数是否为空列表,其他所有参数均为类型检查,即检查其参数是否为给定类型。

Could you please provide some examples where these entries will be used? 您能否提供一些使用这些条目的示例?

Functions returning booleans are most often used in if or cond conditions, so you might see something like this as an example of a function using null? 返回布尔值的函数最常在ifcond条件下使用,因此您可能会看到这样的例子作为使用null?的函数的示例null? :

(define (sum lst)
  (if (null? lst)
    0
    (+ (first lst) (sum (rest lst)))))

I'm not familiar with Lisp, but looking at the definitions of these terms, this looks like these are being used to check what the parameters are, ie 'list?' 我对Lisp并不熟悉,但是看一下这些术语的定义,就好像它们被用来检查参数是什么,即“列表”? maps to a function that tests if the parameter is a list, 'procedure?' 映射到一个测试该参数是否为列表的函数,“过程?” tests if the parameter is a procedure (or at least is callable), 'null?' 测试参数是否为过程(或至少是可调用的),是否为null? tests for an empty list (perhaps this is how nulls are represented in the interpreter), etc. 测试一个空列表(也许这就是解释器中表示空值的方式),等等。

In most Lisp dialects, identifiers (actually, symbol names) can contain punctuation like ? 在大多数Lisp方言中,标识符(实际上是符号名称)可以包含标点符号,例如? (or ! or - ) (或!-

Conventionally, in Scheme, predicate names (naming functions returning a testable boolean) have their name ending with ? 按照惯例,在Scheme中, 谓词名称(返回可测试布尔值的命名函数)的名称以?结尾? (for example list? ), and side effecting functions have their name ending with ! (例如list? ),并且副作用函数的名称以!结尾! (for example vector-set! ); (例如vector-set! ); see also R5RS and SICP . 另请参见R5RSSICP

In Common Lisp, predicate names often end with P (eg listp ) 在Common Lisp中, 谓词名称通常以P结尾(例如listp

These naming conventions are just useful conventions. 这些命名约定只是有用的约定。 You could name your function foo!bar?x even if it is not a predicate. 您可以将函数命名为foo!bar?x即使它不是谓词也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM