我正在阅读以下文档,以在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),
})
'是什么?'最后的关键是什么意思?您能否提供一些使用这些条目的示例?
答案 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)