Frage

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

War es hilfreich?

Lösung

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].
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top