Prolog:リストに関する質問
prefix(L1, L2):L1がL2のサブリストかどうか調べる述語を作ってるんですがサブリストに含まれているものしか判定できないまま悩み続けています。含まれていないものを入力すると無限ループに入ります。compoundを使ってみてもRedoでその行だけで無限ループに入ります。。。
なにか打開策はないでしょうか?
/*---------------------無限ループ------------------------*/
[debug] ?- trace, prefix([c], [a, b, c]).
Call: (8) prefix([c], [a, b, c]) ? creep
Call: (9) prefix([c], [b, c]) ? creep
Call: (10) prefix([c], [c]) ? creep
Exit: (10) prefix([c], [c]) ? creep
Exit: (9) prefix([c], [b, c]) ? creep
Exit: (8) prefix([c], [a, b, c]) ? creep
Yes
[debug] ?-
| .
ERROR: Stream user_input:0:30 Syntax error: Unexpected end of clause
[debug] ?- trace, prefix([g], [a, b, c]).
Call: (8) prefix([g], [a, b, c]) ? creep
Call: (9) prefix([g], [b, c]) ? creep
Call: (10) prefix([g], [c]) ? creep
Call: (11) prefix([g], []) ? creep
Call: (12) prefix([g], []) ? creep
Call: (13) prefix([g], []) ? creep
Call: (14) prefix([g], []) ? creep
Call: (15) prefix([g], []) ? creep
Call: (16) prefix([g], []) ? creep
Call: (17) prefix([g], []) ?
/*-----------------Redo-----------------*/
?- trace, prefix([g], [a, b, c]).
Call: (9) prefix([g], [a, b, c]) ? cree
Call: (10) compound([b, c]) ? creep
Exit: (10) compound([b, c]) ? creep
Call: (10) prefix([g], [b, c]) ? creep
Call: (11) compound([c]) ? creep
Exit: (11) compound([c]) ? creep
Call: (11) prefix([g], [c]) ? creep
Call: (12) compound([]) ? creep
Fail: (12) compound([]) ? creep
Redo: (11) prefix([g], [c]) ? creep
Call: (12) prefix([g], [c]) ? creep
Call: (13) compound([]) ? creep
Fail: (13) compound([]) ? creep
Redo: (12) prefix([g], [c]) ? creep
Call: (13) prefix([g], [c]) ? creep
Call: (14) compound([]) ? creep
Fail: (14) compound([]) ? creep
Redo: (13) prefix([g], [c]) ? creep
Call: (14) prefix([g], [c]) ? creep
Call: (15) compound([]) ? creep
Fail: (15) compound([]) ? creep
Redo: (14) prefix([g], [c]) ? creep
Call: (15) prefix([g], [c]) ? creep
Call: (16) compound([]) ? creep
Fail: (16) compound([]) ? creep
/*-------------ソース----------------*/
prefix([], _).
prefix([X], [X|_]).
prefix([X], [_|Y1]) :- prefix([X], Y1).
prefix([X|Y], Y1) :- prefix([X], Y1), prefix(Y, Y1).
/*-------------compound---------------*/
prefix([], _).
prefix([X], [X|_]).
prefix([X], [_|Y1]) :- compound(Y1), prefix([X], Y1).
prefix([X|Y], Y1) :- prefix([X], Y1), prefix(Y, Y1).