How to check which items in the list meet certain conditions?

How to create a function called busLineLonger that takes at least two parameters to decide if the bus line is long or not?

*/This is how it works*/
* busStops(number_of_the_bus,number_of_stops)*/

/*?- busLineLonger([busStops(1,7),busStops(2,4),busStops(3,6)],5,WHICH).
* WHICH = [1,3].

Using only comparative things like @> <@ / == @. Sorry my english

Edit ... So far, I'm thinking of something like this.

busLineLonger([busStops(A,B)|R],N,[_|_]):-
   N@>B,
   busLineLonger(R,N,A).
+1
source share
2 answers

Here's how you could do this using , proven test predicates , and lambda expressions .

:- use_module(library(lambda)).

First, we define a reified test predicate (>)/3as follows:

>(X,Y,Truth) :- (X > Y -> Truth=true ; Truth=false).

busLineLonger/3 ( busLineLonger1/3, busLineLonger2/3 busLineLonger3/3) -: maplist/3, tfilter/3, tfiltermap/4, tchoose/3. , - , !

# 1: tfilter/3 maplist/3

:  1. , .  2. .

busLineLonger1(Ls0,N,IDs) :-
    tfilter(\busStops(_,L)^(L>N), Ls0,Ls1),
    maplist(\busStops(Id,_)^Id^true, Ls1, IDs).

# 2: tfiltermap/4

-, , - - tfiltermap/4. .

busLineLonger2(Ls,N,IDs) :-
    tfiltermap(\busStops(_,L)^(L>N), \busStops(Id,_)^Id^true, Ls,IDs).

tfiltermap/4 :

:- meta_predicate tfiltermap(2,2,?,?).
tfiltermap(Filter_2,Map_2,Xs,Ys) :-
   list_tfilter_map_list(Xs,Filter_2,Map_2,Ys).

:- meta_predicate list_tfilter_map_list(?,2,2,?).
list_tfilter_map_list([],_,_,[]).
list_tfilter_map_list([X|Xs],Filter_2,Map_2,Ys1) :-
   if_(call(Filter_2,X), (call(Map_2,X,Y),Ys1=[Y|Ys0]), Ys1=Ys0),
   list_tfilter_map_list(Xs,Filter_2,Map_2,Ys0).

# 3: tchoose/3

-, .

busLineLonger3(Ls,N,IDs) :-
    tchoose(\busStops(Id,L)^Id^(L>N), Ls,IDs).

tchoose/3 :

:- meta_predicate tchoose(3,?,?).
tchoose(P_3,Xs,Ys) :-
   list_tchoose_list(Xs,P_3,Ys).

:- meta_predicate list_tchoose_list(?,3,?).
list_tchoose_list([],_,[]).
list_tchoose_list([X|Xs],P_3,Ys1) :-
   if_(call(P_3,X,Y), Ys1=[Y|Ys0], Ys1=Ys0),
   list_tchoose_list(Xs,P_3,Ys0).

!

?- Xs = [busStops(1,7),busStops(2,4),busStops(3,6)], busLineLonger1(Xs,5,Zs).
Xs = [busStops(1, 7), busStops(2, 4), busStops(3, 6)],
Zs = [1, 3].

?- Xs = [busStops(1,7),busStops(2,4),busStops(3,6)], busLineLonger2(Xs,5,Zs).
Xs = [busStops(1, 7), busStops(2, 4), busStops(3, 6)],
Zs = [1, 3].

?- Xs = [busStops(1,7),busStops(2,4),busStops(3,6)], busLineLonger3(Xs,5,Zs).
Xs = [busStops(1, 7), busStops(2, 4), busStops(3, 6)],
Zs = [1, 3].

!

... ?

  • - , , .
  • - - , .
  • - " ", .
  • - ( ), " -".
    • - , , .
    • , , # 3 (, tchoose/3).
+2

:

  • [_|_], ... . : , B , N, ; , B N, .
  • . , ?

:

busLineLonger([],_,[]).
busLineLonger([busStops(A,B)|R],N,[A|S]) :- B>N, busLineLonger(R,N,S).
busLineLonger([busStops(_,B)|R],N,S) :- B=<N, busLineLonger(R,N,S).

?- busLineLonger([busStops(1,7),busStops(2,4),busStops(3,6)],5,WHICH).
WHICH = [1, 3]
0

All Articles