I want to split an integer from an atom. Any ideas how I could do that?
Sample query:
?- split_int('nc(4)', N). % given: the atom 'nc(4)'
N = 4. % expected: the integer 4
I want to split an integer from an atom. Any ideas how I could do that?
Sample query:
?- split_int('nc(4)', N). % given: the atom 'nc(4)'
N = 4. % expected: the integer 4
In SWI Prolog, you should be able to say something like
?- term_to_atom( nc(N) , 'nc(4)' ).
N = 4.
and get what you want. In Sicstus, it looks like you need to use library(codesio)
. That should let you say something like this:
atom_to_term( A , T ) :-
atom_codes( A , Cs ) ,
read_from_codes( Cs , T )
.
Though you'll have to ensure your atom is terminated by a period/full stop. 'nc(4)'
won't work, but 'nc(4).'
will work.
Wrote the below program by using SWI prolog 6,
atom_chars => convert to char list,
and process the char list to get the number characters
and use atom_chars/2, atom_number/2 to change it back to number
https://github.com/neojou/prolog/blob/master/examples/split_int.pl
?- split_int('nc(4)', N). N = 4.
?- split_int('nc(1234)', N). N = 1234.