2

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
Audrius Kažukauskas
  • 13,173
  • 4
  • 53
  • 54
  • 4
    `?- atom_to_term('nc(4)', nc(N), []).` yields: **N = 4**. – mat Feb 08 '16 at 19:02
  • @Giththan without quotes like `nc(4)` you can use `split_int(Fact,N) :- arg(1,Fact,N).` – Ans Piter Feb 08 '16 at 22:35
  • @mat `atom_to_term/3` is it available in SICStus-prolog,if not is there other alternative ? – Ans Piter Feb 08 '16 at 22:48
  • 1
    Check it out: SICStus-based [emulation](http://stackoverflow.com/q/20165720/1613573)! – mat Feb 08 '16 at 22:51
  • @mat thx,what I found in [link](http://stackoverflow.com/questions/20165720/how-to-simulate-atom-to-termatom-term-bindings-of-swi-prolog-in-sicstus-p) is for the double_quotes , not for **' '** like `'foo'` --> foo – Ans Piter Feb 08 '16 at 23:05
  • @mat Thank you for your answers and comments. –  Feb 09 '16 at 01:51
  • @Ans Piter Thank you for your answer,Its also useful. –  Feb 09 '16 at 01:52

2 Answers2

0

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.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
0

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.

Neo Jou
  • 61
  • 2