Rearranging variable_names

后端 未结 4 1152
有刺的猬
有刺的猬 2021-01-01 08:17

How to write in a standard conforming manner avs_term_rearranged(AVs, T, AVsR) with given AVs and T such that AVsR is a p

4条回答
  •  隐瞒了意图╮
    2021-01-01 09:02

    Use term_variables/2 on T to obtain a list Xs with variables in the desired order. Then build a list with the elements of AVs, but in that order.

    avs_term_rearranged(AVs, T, AVRs) :-
        term_variables(T, Xs),
        pick_in_order(AVs, Xs, AVRs).
    
    pick_in_order([], [], []).
    pick_in_order(AVs, [X|Xs], AVRs) :-
        ( pick(AVs, X, AV, AVs1) ->
            AVRs = [AV|AVRs1],
            pick_in_order(AVs1, Xs, AVRs1)
        ;
            pick_in_order(AVs, Xs, AVRs)
        ).
    
    pick([AV|AVs], X, AX, DAVs) :-
        (_=V) = AV,
        ( V==X ->
            AX = AV,
            DAVs = AVs
        ;
            DAVs = [AV|DAVs1],
            pick(AVs, X, AX, DAVs1)
        ).
    

    Notes:

    • this is quadratic because pick/4 is linear
    • term_variables/2 is not strictly necessary, you could traverse T directly

提交回复
热议问题