如何在不使用integer/1
(HW) 的情况下检查字符是否是 Prolog 中的数字?
我可以使用atomic/1
,但它对我没有多大帮助,因为它也返回 true
除了数字之外的任何东西。
2 ?- atomic(1).
true.
3 ?- atomic(asasa).
true.
4 ?-
问候
Use char_type(X,digit).
?- char_type('3',digit).
true.
?- char_type('a',digit).
false.
Well, what do you consider a number? Just integers? Hex numbers? In case you want to recognise just plain integers ([0-9]+
) you can just convert the number to a list and then check if each member of the list is a digit:
is_digit(1).
is_digit(2).
....
Another idea is trying to add 1
to the number using is
; but this will not always guarantee that it's an integer.