ฉันกำลังพยายามเขียนเพรดิเคตเพื่อค้นหาความลึกของการซ้อนคำในอารัมภบท ตัวอย่างเช่น : สำหรับอะตอมหรือตัวแปร ความลึกจะเป็นศูนย์ สำหรับ f(a,b,1,2) ความลึกคือ 1 สำหรับ f(a,b(7,a),1,2) ความลึกคือ 2 เป็นต้น นี่คือสิ่งที่ฉันมีจนถึงตอนนี้
% base cases.
get_depth(Term,0):-
non_compound(Term),!.
get_depth(Term,1):-
Term =.. [_|T],
all_basic(T),!. % no compound terms in list.
get_depth(Term,Depth):-
% this is where I need help.
% helper prdeicates
all_basic([T]):-
non_compound(T),!.
all_basic([H|T]):-
non_compound(H),
all_basic(T).
% term is non compound, either atomic or non instantiated variable.
non_compound(Term):-
atomic(Term),!;
var(Term).
max(X,Y,X):-
X >= Y,!.
max(_,Y,Y).
?- term_string(a(b(C,D),e(F),g),String). String = "a(b(_10232,_10234),e(_10238),g)"
กล่าวอีกนัยหนึ่ง อย่าถือว่าอินพุตเป็นคำศัพท์ Prolog ประเภทใดประเภทหนึ่งโดยเฉพาะ แต่ถือเป็นลำดับของอักขระแทน - person Guy Coder   schedule 26.11.20201+1
คืออะไร ฉันจะบอกว่า 1 เนื่องจากเป็นรูปแบบกะทัดรัดของ+(1, 1)
- person Willem Van Onsem   schedule 26.11.2020a()
? - person Willem Van Onsem   schedule 26.11.20201+1
คืออะไร? ความลึกของa()
คืออะไร? นอกจากนี้หากมีการแปลงรายการเป็นเครื่องหมายจุดเช่น?- write_term([a,b,c], [dotlists(true)]). .(a,.(b,.(c,[]))) true.
ดังนั้นเราควรแปลงอินพุตเป็น AST ก่อนประมวลผลหรือไม่ - person Guy Coder   schedule 26.11.2020not use any specific prolog built in
เมื่อเวลาผ่านไป - person Guy Coder   schedule 26.11.2020