0
votes

I have a List of Lists, let's call it List1 like:

[[0.001, 0.005], [0.004, 0.010], [0.123, 0.501]]

I also have another list, let's call it List2 like: [0.002, 0.008, 0.655]

I need to check if nth number of List2 is between the numbers of List1 and do something if it is, and do something else if it is not (assuming the first number in the number pairs of List1 is always smaller than the second number of the pair.)

Example: 0.002 is between 0.001 and 0.005 So, do something 0.655 is not between 0.123 and 0.501 So, do something else

I really tried everything I can think of but could not find a solution. A help would be amazing!

1
So, let's give the predicate a name: intervals_numbers and can you complete the case for a single number? The shape is something like: intervals_numbers([[Left,Right]], [Number]) :- .....lambda.xy.x

1 Answers

0
votes
% to choose from
is_inside_inclusive(N, [S, G]) :- N >= S, N =< G.
is_inside_left_inclusive(N, [S, G]) :- N >= S, N < G.
is_inside_right_inclusive(N, [S, G]) :- N > S, N =< G.
is_inside_exclusive(N, [S, G]) :- N > S, N < G.

do_something(_) :-
    write('do something\n').
    
do_something_else(_) :-
    write('do something else\n').


my_predicate([Interval|IT], [Number|NT]) :-
    is_inside_inclusive(Number, Interval),
    do_something(Number),
    my_predicate(IT,NT).
my_predicate([Interval|IT], [Number|NT]) :-
    not(is_inside_inclusive(Number, Interval)),
    do_something_else(Number),
    my_predicate(IT,NT).