문제

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