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

有帮助吗?

解决方案

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].
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top