Prolog:单项与单项列表

时间:2011-09-07 05:54:29

标签: prolog

在对学校的大型作业进行故障排除时,我发现了一个错误,我在处理单个项目列表(一个项目的堆栈),好像它是一个单独的项目。我解决了我的问题,但是在进一步的测试中我发现了一些奇怪的东西:

48 ?- 1 is [1].
true.

49 ?- -1 is [-1].
ERROR: is/2: Type error: `character' expected, found `-1'

50 ?- 0.66 is [0.66].
ERROR: is/2: Type error: `character' expected, found `0.66'

使用=:= / 2而不是/ 2发生类似的行为。因此,无论出于何种原因,单个项目列表被视为与单个项目相同,但仅适用于非负整数。

好奇心比什么都重要......有谁知道这是为什么?

谢谢!

1 个答案:

答案 0 :(得分:4)

SWI-Prolog(或许是其他人)中,这与is/2=:=/2进行评估的backward compatibility implementation个表达式相关:

.(+Int,[])

A list of one element evaluates to the element. This implies "a" evaluates to 
the character code of the letter `a' (97). This option is available for 
compatibility only. It will not work if `style_check(+string)' is active as "a"
will then be transformed into a string object. The recommended way to specify the
character code of the letter `a' is 0'a.

由于字符代码是非负整数,这可以解释为什么您所看到的行为仅适用于此类数字,而不适用于浮点数和负数。