How to make a function called busLineLonger, which receives at least two parameters to decide if a bus line is longer 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've think of something like this
busLineLonger([busStops(A,B)|R],N,[_|_]):-
N@>B,
busLineLonger(R,N,A).
Here's how you could do it using meta-predicates, reified test predicates, and lambda expressions.
First, we define the reified test predicate
(>)/3
like this:Next, we define three different implementations of
busLineLonger/3
(namedbusLineLonger1/3
,busLineLonger2/3
, andbusLineLonger3/3
) in terms of the following meta-predicates:maplist/3
,tfilter/3
,tfiltermap/4
, andtchoose/3
. Of course, in the end we will only need one---but that shouldn't keep us from exploring the various options we have!#1: based on
tfilter/3
andmaplist/3
Do two separate steps: 1. Select items of concern. 2. Project those items to the data of interest.
#2: based on
tfiltermap/4
Here, we use exactly the same lambda expressions as before, but we pass them both to meta-predicate
tfiltermap/4
. Doing so can help reduce save some resources.Here's how
tfiltermap/4
can be implemented:#3: based on
tchoose/3
Here we do not use two separate lambda expressions, but a combined one.
Here's how
tchoose/3
can be implemented:Let's see them in action!
Done!
So... what's the bottom line?
tchoose/3
) is best.Some things to fix in your code:
[_|_]
, that is the result are free variables... doesn't make sense. You need two cases: one in which theB
is greater thanN
and you include the result; the other in whichB
is less or equal thanN
, and you don't include that result.A possible solution: