【发布时间】:2015-11-22 21:17:32
【问题描述】:
我编写了用于计数零值节点的谓词,但计数器 Num1 工作不正确。
countZeros(empty, Num).
countZeros(tree(0, Left, Right), Num) :-
Num1 = Num + 1,
countZeros(Left, Num1),
countZeros(Right, Num1).
countZeros(tree(_, Left, Right), Num) :-
countZeros(Left, Num),
countZeros(Right, Num).
这是我的疑问:
countZeros(tree(5,
tree(0,
tree(6, empty, empty),
tree(4, empty, empty)
),
tree(0,
tree(2, empty, empty),
tree(0, empty, empty)
)
)
, 0).
谁能帮忙?
【问题讨论】:
-
请解释这段代码在哪些方面是“不正确的”。
-
Num1 is Num + 1,而不是Num1 = Num + 1。=/2是一个统一运算符,而不是赋值。除非你使用 Visual/Turbo/PDC Prolog。 -
@lurker 我在 Visual Prolog 中编写,
is而不是=会导致错误。 -
@ScottHunter Num1 在程序结束时等于 2。
-
@dedmarkel 好的,然后忽略我的评论。 Visual Prolog 不遵循标准。
标签: tree prolog binary-tree