Say I have a List [1,2,4,5], I would like to have a predicate that returns 3 as the missing element. You can assuming that the input list is always sorted sequentially.
My solution so far:
% missing_number/2 (ListToBeChecked, ListToBeCompared, MissingNum)
missing_number([], [], []) :- !.
missing_number([Head | Tail], [Head | Rest], Number) :-
missing_number(Tail, Rest, Number).
missing_number(_, [X | _], [X | Node]) :-
missing_number(_, _, Number), !.
Use
between/3to generate all numbers from min to max. Usememberchk/2(ormember/2) to find the missing ones.Exercise for the reader: wrap this up in a predicate.
EDIT Efficient solution, by popular request:
EDIT 2: More elegant version of the above, inspired by @chac, not necessarily very efficient: