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 而不是 is/2 会发生类似的行为。因此,无论出于何种原因,单个项目列表被视为与单个项目相同,但仅限于非负整数。
好奇心比什么都重要......有人知道这是为什么吗?
谢谢!
While troubleshooting a larger assignment for school, I found a mistake I had made, where I was treating a single item list (a stack with one item) as if it were a single item. I solved my issue, however in further testing I noticed something weird:
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'
Similar behavior happens using =:=/2 instead of is/2. So for whatever reason, a single item list is considered the same as a single item, but only for non-negative integers.
Curiosity more than anything else... anybody know why this is?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 SWI-Prolog(也许还有其他)中,这与 向后兼容性实现用于通过
is/2
和=:=/2
求值的表达式:由于字符代码是非负整数,这可以解释为什么您看到的行为仅适用于此类数字,而不适用于浮点数和负数。
In SWI-Prolog (and perhaps others), this is related to a backward compatibility implementation of expressions for evaluation by
is/2
and=:=/2
:As character codes are non-negative integers, this may explain why the behaviour you're seeing only works for such numbers and not floating point and negative numbers.