첫 번째 해결 방법은 올바른 목록을 작성하더라도 반환 할 목록 변수의 이름 대신 모든 항목과 통합되는 익명 변수 '_'을 전달하는 것입니다. 종료.
splitintowords(CharList, WordList):- splitintowords_(CharList, [], [], WordList).
splitintowords_([],[], Res, Res).
splitintowords_([],Word, WordList,L):-
\+ Word = [],
append([Word], WordList, NWordList),
splitintowords_([],[], NWordList,L).
splitintowords_([H|T],Word, WordList,L):-
is_alpha(H),
append([H], Word, NWord),
splitintowords_(T,NWord, WordList,L).
splitintowords_([H|T],Word, WordList,L):-
\+ is_alpha(H),
append([Word], WordList, NWordList),
splitintowords_(T, [], NWordList,L).
: 코드를 부여 T,하지만 약간의 변화와 같은 .... 코드해야한다, 당신의 천국이 때문에
나는/1 대신 알파벳의 술어 is_alpha에서 빌드 솔루션을 시도 예 : 솔루션이 보여 매우 크기 때문에 ...., 당신이
w
를 눌러 전체 솔루션을 볼 수
string_codes('hello there, how are you?',T), splitintowords(T, W).
T = [104, 101, 108, 108, 111, 32, 116, 104, 101|...],
W = [[117, 111, 121], [101, 114, 97], [119, 111, 104], [], [101, 114, 101, 104|...], [111, 108, 108|...]] ;
false.
참고 SWI - 프롤로그 :
?- string_codes('hello there, how are you?',T), splitintowords(T, W).
T = [104, 101, 108, 108, 111, 32, 116, 104, 101|...],
W = [[117, 111, 121], [101, 114, 97], [119, 111, 104], [], [101, 114, 101, 104|...], [111, 108, 108|...]] [write]
T = [104, 101, 108, 108, 111, 32, 116, 104, 101, 114, 101, 44, 32, 104, 111, 119, 32, 97, 114, 101, 32, 121, 111, 117, 63],
W = [[117, 111, 121], [101, 114, 97], [119, 111, 104], [], [101, 114, 101, 104, 116], [111, 108, 108, 101, 104]] ;
false.
(두 번째 T, W는 첫 번째 T, W의 전체 솔루션입니다.)
또한 두 번째 솔루션 이와 유사한 변화 :
splitintowords(CharList, WordList):-
splitintowords_(CharList, [[]], WordList).
splitintowords_([], WordList, WordList).
splitintowords_([H|T], [Word|Words], L):-
is_alpha(H),
WordN = [H|Word],
splitintowords_(T, [WordN|Words], L).
splitintowords_([H|T], Words,L):-
\+ is_alpha(H),
WordsN = [[]|Words],
splitintowords_(T, WordsN,L).
예 : 어딘가에 중간에 그것은 이전 솔루션에 비해 하나 개의 빈 목록이 더있다
?- string_codes('hello there, how are you?',T), splitintowords(T, W).
T = [104, 101, 108, 108, 111, 32, 116, 104, 101, 114, 101, 44, 32, 104, 111, 119, 32, 97, 114, 101, 32, 121, 111, 117, 63],
W = [[], [117, 111, 121], [101, 114, 97], [119, 111, 104], [], [101, 114, 101, 104, 116], [111, 108, 108, 101, 104]] ;
false.
참고. 당신은 빈 목록을 제거 할 경우 한 번 더 술어를 작성하여 매우 쉽게 될 것이다 :
delete([],[]).
delete([H|T],[H|T1]):-dif(H,[]),delete(T,T1).
delete([[]|T],T1):-delete(T,T1).
예 :
?- delete([[], [117, 111, 121], [101, 114, 97], [119, 111, 104], [], [101, 114, 101, 104, 116], [111, 108, 108, 101, 104]],L).
L = [[117, 111, 121], [101, 114, 97], [119, 111, 104], [101, 114, 101, 104, 116], [111, 108, 108, 101, 104]] ;
false.
은 (그것은 빈 목록을 제거).
편집
나는 또한 당신의 술어 alphabetic/1
와 함께 테스트 is_alpha/1
와 정확히 같은 출력을 생성합니다.
은 영문자/1을 사용하시기 바랍니다. – coder
알파벳순 (N) : - char_type (N, alpha). – ejbs
이 예제의 단어는 무엇입니까? '여보세요, 저기, 어떻게, 있니? ' –