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

时间:2018-12-26 10:50:06

标签: python lisp

我正在阅读以下文档,以在Python中实现Lisp解释器:http://norvig.com/lispy.html

standard_env 函数中,已定义了一个词典,用于将某些符号或变量映射到其相应的函数或值。但是,我无法理解 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),
})

'是什么?'最后的关键是什么意思?您能否提供一些使用这些条目的示例?

3 个答案:

答案 0 :(得分:5)

从语法上讲,#' documentation ought to be here stat_shadows <- function(mapping = NULL, data = NULL, geom = "shadows", position = "identity", ..., # do I need to add the geom_shadows arguments here? anchor = list(x = 0, y = 0), shadows = list("x", "y"), type = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) { layer( stat = StatShadows, data = data, mapping = mapping, geom = geom, position = position, show.legend = show.legend, inherit.aes = inherit.aes, params = list( # geom_shadows argument repeated here? anchor = anchor, shadows = shadows, type = type, na.rm = na.rm, ... ) ) } StatShadows <- ggproto("StatShadows", Stat, # do I need to repeat required_aes? required_aes = c("x", "y"), # set up the data, e.g. remove missing data setup_data = function(data, params) { data }, # set up parameters, e.g. unpack from list setup_params = function(data, params) { params }, # calculate shadows: returns data_frame with colnames: xmin, xmax, ymin, ymax compute_group = function(data, scales, anchor = list(x = 0, y = 0), shadows = list("x", "y"), type = NULL, na.rm = TRUE) { .compute_shadows(data = data, anchor = anchor, shadows = shadows, type = type) } ) # Calculate the shadows for each type / shadows / anchor .compute_shadows <- function(data, anchor, shadows, type) { # Deleted all type-checking, etc. for MWE # Only 'type = c(double, double)' accepted, e.g. type = c(0, 1) qs <- type # compute shadows along the x-axis if (any(shadows == "x")) { shadows.x <- c( xmin = as.numeric(stats::quantile(data[, "x"], qs[[1]])), xmax = as.numeric(stats::quantile(data[, "x"], qs[[2]])), ymin = anchor[["y"]], ymax = anchor[["y"]]) } # compute shadows along the y-axis if (any(shadows == "y")) { shadows.y <- c( xmin = anchor[["x"]], xmax = anchor[["x"]], ymin = as.numeric(stats::quantile(data[, "y"], qs[[1]])), ymax = as.numeric(stats::quantile(data[, "y"], qs[[2]]))) } # store shadows in one data_frame stats <- new_data_frame(c(x = shadows.x, y = shadows.y)) # return the statistics stats } . 只是Lisp标识符中允许的许多字符之一。因此,它只是函数名称的一部分。

按约定,以?结尾的函数名称用于返回布尔值的函数。

具体地说,?检查其两个参数是否相等,equal?检查其参数是否为空列表,所有其他参数均为​​类型检查,即它们检查其参数是否为给定类型

  

能否请您提供一些使用这些条目的示例?

返回布尔值的函数最常在null?if条件下使用,因此您可能会看到类似这样的内容作为使用cond的函数的示例:

null?

答案 1 :(得分:1)

我对Lisp并不熟悉,但是查看这些术语的定义,就好像这些术语正在被用来检查参数是什么,即“列表”?映射到一个测试该参数是否为列表的函数,“过程?”测试参数是否为过程(或至少是可调用的),是否为null?测试一个空列表(也许这就是解释器中表示空值的方式),等等。

答案 2 :(得分:1)

在大多数Lisp方言中,标识符(实际上是symbol名)可以包含?(或!-)之类的标点符号

通常,在Scheme中,谓词名称(返回可测试布尔值的命名函数)的名称以?结尾(例如list?),而副作用函数具有其名称以!结尾(例如vector-set!);另请参见R5RSSICP

在Common Lisp中,predicate名称通常以P结尾(例如listp

这些命名约定只是有用的约定。您可以为函数foo!bar?x命名,即使它不是谓词。