0

I have the following:

is_digit(X):-char_type(X,digit).

When I call it like this: is_digit(X).

I get the folloring results:

X='0'; 
X='1'; 
... ; 
X='9'

I need to get those same results but without the quotes. Excuse me if it is a simple question but I just haven't been able to find a way around this. Thanks!

false
  • 10,264
  • 13
  • 101
  • 209
Soph
  • 2,895
  • 5
  • 37
  • 68

3 Answers3

2
?- between(0, 9, X).
X = 0 ;
X = 1 ;
X = 2 ;
X = 3 ;
X = 4 ;
X = 5 ;
X = 6 ;
X = 7 ;
X = 8 ;
X = 9.
Kaarel
  • 10,554
  • 4
  • 56
  • 78
1

If you want the number use atom_number(A,N). i.e.

?- char_type(X,digit),atom_number(X,N).
X = '0',
N = 0 ;
X = '1',
N = 1 ;
X = '2',
N = 2 ;
X = '3',
CapelliC
  • 59,646
  • 5
  • 47
  • 90
1

If you want to be portable among ISO Prolog implementations, you need to use number_chars/2. atom_number/2 exists only in SWI, YAP, Ciao. But number_chars/2 is supported by those 3 and IF, B, GNU, SICStus, XSB, Jekejeke.

?- X = '1', number_chars(N, [X]).
   X = '1', N = 1.
false
  • 10,264
  • 13
  • 101
  • 209
  • ?- number_chars(2,X). yields X = ['2']. not a great improvement for the OP. Instead the combination of 2 ISO predicates atom_codes/2 and number_codes/2 (i.e. atom_number/2) give better results. – CapelliC Mar 16 '12 at 08:42
  • 1
    @chac: OP demanded to convert decimal digits. And as to `atom_number/2`: mentioned 3 systems differ in several cases. So better stick to ISO and insist on conformance. – false Mar 16 '12 at 14:46