开发者

Prolog: single item vs single item list

开发者 https://www.devze.com 2023-04-03 08:10 出处:网络
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, howev

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 i开发者_如何学Cntegers.

Curiosity more than anything else... anybody know why this is?

Thanks!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号