Use a pair instead of a list to represent intervals
pmoura opened this issue · comments
Representing an interval using a pair, e.g. (X,Y)
is more efficient than using a list, [X,Y]
. To make it clear:
| ?- write_canonical([1,7]).
'.'(1,'.'(7,[]))
yes
| ?- write_canonical((1,7)).
','(1,7)
yes
I.e. accessing the upper limit in an interval is more costly with a list representation.
Thanks Paulo. This is a nice suggestion actually.
I might be needing some ways to get the first or the second element of the pair in the future. I know how to do this with lists, but I'm not sure how this could be possible with pairs. I'll look it up.
As the interval_arithmetic
is I believe it can be changed easily to use pairs instead of lists. So I'll get to it.
I found this suggestion regarding my question above.
I might be needing some ways to get the first or the second element of the pair in the future. I know how to do this with lists, but I'm not sure how this could be possible with pairs. I'll look it up.
Not sure I understand the problem. You can simply use unification as you're doing with lists. E.g.
is_in((Xa, Xb), Number) :-
Xa =< Number,
Xb >= Number.
I might be needing some ways to get the first or the second element of the pair in the future. I know how to do this with lists, but I'm not sure how this could be possible with pairs. I'll look it up.
Not sure I understand the problem. You can simply use unification as you're doing with lists. E.g.
is_in((Xa, Xb), Number) :- Xa =< Number, Xb >= Number.
I wasn't thinking about the predicates from interval_arithmetic
, but for predicates to be created (see #10).
But it seems that unification should be enough. I'll have to think things harder, it seems. Thanks !
The interval_arithmetic
library has been converted to use pairs (tuples) instead of lists.
So far all the created tests pass with 100%.
For this reason I'll close this issue.