-
I am struggling to remove the redundant choice point in :- use_module(library(lists)).
:- use_module(library(reif)).
:- use_module(library(debug)).
ints_median_index(Ints, Median, Index) :-
length(Ints, N),
Index #= N // 2,
Index1 #= Index + 1,
if_(clpz_t( N mod 2 #= 0),
( nth1(A, Ints, Index),
nth1(B, Ints, Index1),
Median is (A + B) / 2
),
nth1(Median, Ints, Index1)
).
?- ints_median_index([1, 2, 3], Median, Index).
%@ Median = 2, Index = 1
%@ ; false.
?- ints_median_index([1, 2, 3, 4], Median, Index).
%@ Median = 2.5, Index = 2
%@ ; false. I have a suspicion this has something to do with argument indexing, because when I use |
Beta Was this translation helpful? Give feedback.
Answered by
jjtolton
Sep 18, 2024
Replies: 1 comment 3 replies
-
Currently, we get: ?- ints_median_index([0], Median, Index). false, unexpected. ?- ints_median_index([0,1], Median, Index). false, unexpected. ?- ints_median_index([0,1,4], Median, Index). false, unexpected. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Whoops... I was confusing the order of the arguments for
nth1/3
🤦