Most general higher-order constraint describing a sequence of integers ordered with respect to a relation

后端 未结 3 1540
时光取名叫无心
时光取名叫无心 2020-11-22 04:16

In CLP(FD), we frequently need to state: \"This is a list of integers and finite domain variables in (sometimes: strictly) ascending/descending order.\"

Is

3条回答
  •  一整个雨季
    2020-11-22 05:08

    Quite in the same vein as mapadj/4 presented in an earlier answer... maybe the name is better.

    forallAdj(P_2,Xs) :-
       list_forallAdj(Xs,P_2).
    
    list_forallAdj([],_).
    list_forallAdj([X|Xs],P_2) :-
       list_forallAdj_prev(Xs,P_2,X).
    
    list_forallAdj_prev([],_,_).
    list_forallAdj_prev([X1|Xs],P_2,X0) :-
       call(P_2,X0,X1),
       list_forallAdj_prev(Xs,P_2,X1).
    

    Sample use:

    :- use_module(library(clpfd)).
    :- use_module(library(lambda)).
    
    ?- Ls = [0,_,_,_,_,_], forallAdj(\X0^X1^(X0 + 1 #= X1), Ls).
    Ls = [0, 1, 2, 3, 4, 5].
    

    Where could that take us?

    • forallAdj => existAdj
    • maybe variants with index (forallAdjI, existAdjI) like in Collections.List Module (F#)
    • findfirstAdj/pickfirstAdj also like F# find/pick

提交回复
热议问题