2

I want to separate number into list digits using prolog

like :
     if number is "345"
separate to [3, 4, 5]

How can i do that ?

stringTokenizer("", []) :- !.
stringTokenizer(Sen, [H|T]) :-
   frontToken(Sen, Token, Remd), H = Token, stringTokenizer(Remd, T).

I am using this predicate to convert string into list of strings

false
  • 10,264
  • 13
  • 101
  • 209
Mahmoud Emam
  • 1,499
  • 4
  • 20
  • 37
  • Do you have `string_to_list` somewhere? If so, you could subtract 48 form each element of the result (assuming your string had nothing by Latin decimal digits). – Ray Toal Jun 23 '12 at 22:18
  • 1
    Here is [a page from a forum describing string_split](http://www.tek-tips.com/viewthread.cfm?qid=280628) and a [Stack Overflow related question](http://stackoverflow.com/questions/3976899/how-to-split-a-sentence-in-swi-prolog). Specific to SWI-Prolog, though. – Ray Toal Jun 23 '12 at 23:51
  • @RayToal - may be you can rephrase your comment as an answer ;-) – Alexander Serebrenik Jul 04 '12 at 18:39

1 Answers1

0

Since there's no explicit answer, you can use the fact that strings are lists of ascii integers to simplify this, as was suggested by @RayToal. Just ensure that all the characters in the string are actually integers.

stringTokenizer([], []).
stringTokenizer([N|Ns], [Digit|Rest]):-
    48 =< N, N =< 57,
    Digit is N - 48,
    stringTokenizer(Ns, Rest).

Your example:

?- stringTokenizer("345", List).
List = [3, 4, 5].
joneshf
  • 2,296
  • 1
  • 20
  • 27