Prolog:单个项目与单个项目列表

发布于 2024-12-03 02:08:32 字数 425 浏览 0 评论 0原文

在对学校的一项较大作业进行故障排除时,我发现自己犯了一个错误,即我将单个项目列表(包含一个项目的堆栈)视为单个项目。我解决了我的问题,但是在进一步的测试中我注意到一些奇怪的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反话 2024-12-10 02:08:32

SWI-Prolog(也许还有其他)中,这与 向后兼容性实现用于通过 is/2=:=/2 求值的表达式:

.(+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.

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

In SWI-Prolog (and perhaps others), this is related to a backward compatibility implementation of expressions for evaluation by is/2 and =:=/2:

.(+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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文