ความลึกของการซ้อนของคำ

ฉันกำลังพยายามเขียนเพรดิเคตเพื่อค้นหาความลึกของการซ้อนคำในอารัมภบท ตัวอย่างเช่น : สำหรับอะตอมหรือตัวแปร ความลึกจะเป็นศูนย์ สำหรับ 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).

person Benny Abramovici    schedule 26.11.2020    source แหล่งที่มา
comment
ทำไมไม่ใช้ term_string/2 แล้วติดตามการเปิดและปิด วงเล็บ? เช่น. ?- 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.2020
comment
@GuyCoder: คำถามคือ ความลึกของ 1+1 คืออะไร ฉันจะบอกว่า 1 เนื่องจากเป็นรูปแบบกะทัดรัดของ +(1, 1)   -  person Willem Van Onsem    schedule 26.11.2020
comment
@GuyCoder: หรือรายการที่มี arity เป็นศูนย์ เช่น a()?   -  person Willem Van Onsem    schedule 26.11.2020
comment
@WillemVanOnsem คำถามที่ดีฉันไม่รู้ นั่นคือเหตุผลที่ความคิดเห็นของฉันถามคำถาม ก็ต้องดูว่าคสช.จะตอบกลับหรือไม่   -  person Guy Coder    schedule 26.11.2020
comment
สารประกอบใดๆ ที่มีความลึก 1 หรือสูงกว่า ขึ้นอยู่กับระดับการซ้อน มิฉะนั้น ถ้าอะตอมหรือตัวแปร ความลึกจะเป็น 0   -  person Benny Abramovici    schedule 26.11.2020
comment
ฉันไม่อยากใช้คำนำเฉพาะใด ๆ ในตัวเช่น term_string เพียงแค่คำนำวานิลลาธรรมดา   -  person Benny Abramovici    schedule 26.11.2020
comment
@BennyAbramovici ดังนั้นความลึกของ 1+1 คืออะไร? ความลึกของ a() คืออะไร? นอกจากนี้หากมีการแปลงรายการเป็นเครื่องหมายจุดเช่น ?- write_term([a,b,c], [dotlists(true)]). .(a,.(b,.(c,[]))) true. ดังนั้นเราควรแปลงอินพุตเป็น AST ก่อนประมวลผลหรือไม่   -  person Guy Coder    schedule 26.11.2020
comment
ฉันให้คะแนนอย่างใกล้ชิดเพราะคำถามต้องการความชัดเจนมากกว่านี้ OP กำลังเพิ่มเงื่อนไขเพิ่มเติม เช่น not use any specific prolog built in เมื่อเวลาผ่านไป   -  person Guy Coder    schedule 26.11.2020
comment
ไม่มีรายการ มีแต่คำศัพท์ในรูปแบบ:t(a,1,f(12,a),5) เป็นต้น   -  person Benny Abramovici    schedule 26.11.2020


คำตอบ (1)


depth(Term, D) :-
    Term =.. [_|Args],
    (  Args = []
    -> D = 0
    ;  maplist(depth, Args, Ds),
       max_list(Ds, D1), D is D1 + 1
    ).

หากคุณไม่ต้องการ maplist และ max_list

depth(Term, D) :-
    Term =.. [_|Args],
    (  Args = []
    -> D = 0
    ;  max_depth(Args, D1), D is D1 + 1
    ).

max_depth([Term], Max) :- depth(Term, Max).
max_depth([T1, T2| Rest], Max) :-
    depth(T1, D1), max_depth([T2 | Rest], M1),
    (D1 > M1 -> Max = D1; Max = M1).
person rajashekar    schedule 26.11.2020
comment
เป็นไปได้ไหมหากไม่มี maplist และ max_list เนื่องจากมีการใช้งานเฉพาะ - person Benny Abramovici; 26.11.2020
comment
คุณสามารถกำหนด maplist และ max_list ของคุณเองได้อย่างง่ายดายเพียงพอหากต้องการ จริงๆ แล้วคุณไม่จำเป็นต้องใช้ทั้งสองอย่าง ฉันคิดว่าการกลิ้ง foldl ของคุณเองก็น่าจะพอแล้ว - person rajashekar; 26.11.2020