【发布时间】:2013-11-26 12:10:05
【问题描述】:
我是 Scheme 编程的初学者。我知道Scheme中的点符号是用来表示一对符号的,例如'(a . b)。
第一个元素可以是符号,也可以是列表,没关系。但是特别是第二个元素必须是一个符号,如果不是,可能是一个列表,那么我们不能用内置的cons过程创建一个对。
那么可以创建一对 2 列表吗???好吧,我正在考虑一个解决方案是将列表转换为符号,但实际上这些是 2 完全不同的事情 -> 据我所知是不可能的。
这是我写的代码:
(define compare-attrs
(lambda (attribute1 attribute2)
(if (or (and (null? attribute1) (null? attribute2)) (and (not (null? attribute1)) (not (null? attribute2))))
(cons attribute1 attribute2)
#f)))
其中attribute1和attribute2是2个列表,我的输出是:
attribute1 atrribute2
预期输出:'(attribute1 .attribute2)
请解释一下。提前致谢!!!
编辑:添加使用 compare-attrs 函数
函数 compare-attrs 用来提取描述实体属性的部分和cons它们组成一对,实体定义如下:
(entity e0 (prov:label "entity e0")
(entity e1 (prov:location "London")
这些实体的属性是(prov:label "entity e0") 和(prov:location "London")。
应用函数 compare-attrs 时,因为这些属性不是null,所以我期望的输出是
`(prov:label "entity e0") . (prov:location "London")`
【问题讨论】:
-
什么是
entity、prov:label和prov:location?我假设它们是函数? -
(entity e0 (prov:label "entity e0"))是图中的一个元素 -
this answer 可能会回答这个问题,它描述了 Scheme 中的点表示法,以及如何打印对。
-
"那么是否可以创建一对 2 个列表??" cdr 是列表的对是列表,所以是的,但它与 car 是列表的列表没有任何不同。 ((a b c) . (d e f)) 的行为与 ((a b c) d e f) 和 ((a. (b. (c .()))) 完全相同。 ((d . (e .(f. () ))) . ())) 它只是没有按照您想要的方式显示。
标签: scheme