2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
:- object(list,
|
|
|
|
implements(listp),
|
|
|
|
extends(compound)).
|
|
|
|
|
|
|
|
|
|
|
|
:- info([
|
2006-12-28 13:03:34 +00:00
|
|
|
version is 1.3,
|
2003-02-05 00:15:28 +00:00
|
|
|
author is 'Paulo Moura',
|
2006-12-28 13:03:34 +00:00
|
|
|
date is 2006/12/21,
|
2001-06-06 20:40:57 +01:00
|
|
|
comment is 'List predicates.']).
|
|
|
|
|
|
|
|
|
|
|
|
:- public(as_difflist/2).
|
|
|
|
|
|
|
|
:- mode(as_difflist(+list, -list), one).
|
|
|
|
|
|
|
|
:- info(as_difflist/2,
|
|
|
|
[comment is 'Converts a list to a difference list.',
|
|
|
|
argnames is ['List', 'Diffist']]).
|
|
|
|
|
|
|
|
|
|
|
|
append([], List, List).
|
|
|
|
|
|
|
|
append([Head| Tail], List, [Head| Tail2]) :-
|
|
|
|
append(Tail, List, Tail2).
|
|
|
|
|
|
|
|
|
|
|
|
as_difflist([], Back-Back).
|
|
|
|
|
|
|
|
as_difflist([Head| Tail], [Head| Tail2]-Back) :-
|
|
|
|
as_difflist(Tail, Tail2-Back).
|
|
|
|
|
|
|
|
|
|
|
|
delete([], _, []).
|
|
|
|
|
|
|
|
delete([Head| Tail], Element, Remaining) :-
|
2006-12-28 13:03:34 +00:00
|
|
|
( Head == Element ->
|
2001-06-06 20:40:57 +01:00
|
|
|
delete(Tail, Element, Remaining)
|
2006-12-28 13:03:34 +00:00
|
|
|
; Remaining = [Head| Tail2],
|
|
|
|
delete(Tail, Element, Tail2)
|
|
|
|
).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
delete_matches([], _, []).
|
|
|
|
|
|
|
|
delete_matches([Head| Tail], Element, Remaining) :-
|
2006-12-28 13:03:34 +00:00
|
|
|
( \+ \+ Head = Element ->
|
2001-06-06 20:40:57 +01:00
|
|
|
delete_matches(Tail, Element, Remaining)
|
2006-12-28 13:03:34 +00:00
|
|
|
; Remaining = [Head| Tail2],
|
|
|
|
delete_matches(Tail, Element, Tail2)
|
|
|
|
).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
empty(List) :-
|
|
|
|
List == [].
|
|
|
|
|
|
|
|
|
|
|
|
flatten(List, Flatted) :-
|
|
|
|
flatten(List, [], Flatted).
|
|
|
|
|
|
|
|
|
|
|
|
flatten(Var, Tail, [Var| Tail]) :-
|
|
|
|
var(Var),
|
|
|
|
!.
|
|
|
|
|
|
|
|
flatten([], Flatted, Flatted) :-
|
|
|
|
!.
|
|
|
|
|
|
|
|
flatten([Head| Tail], List, Flatted) :-
|
|
|
|
!,
|
|
|
|
flatten(Tail, List, Aux),
|
|
|
|
flatten(Head, Aux, Flatted).
|
|
|
|
|
|
|
|
flatten(Head, Tail, [Head| Tail]).
|
|
|
|
|
|
|
|
|
|
|
|
keysort(List, Sorted) :-
|
|
|
|
{keysort(List, Sorted)}.
|
|
|
|
|
|
|
|
|
|
|
|
last([Head| Tail], Last) :-
|
|
|
|
last(Tail, Head, Last).
|
|
|
|
|
|
|
|
|
|
|
|
last([], Last, Last).
|
|
|
|
|
|
|
|
last([Head| Tail], _, Last) :-
|
|
|
|
last(Tail, Head, Last).
|
|
|
|
|
|
|
|
|
|
|
|
length(List, Length) :-
|
2006-12-28 13:03:34 +00:00
|
|
|
( integer(Length) ->
|
2001-06-06 20:40:57 +01:00
|
|
|
Length >= 0,
|
|
|
|
make_list(Length, List)
|
2006-12-28 13:03:34 +00:00
|
|
|
; length(List, 0, Length)
|
|
|
|
).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
make_list(0, []):-
|
|
|
|
!.
|
|
|
|
|
|
|
|
make_list(N, [_| Tail]):-
|
|
|
|
M is N-1,
|
|
|
|
make_list(M, Tail).
|
|
|
|
|
|
|
|
|
|
|
|
length([], Length, Length).
|
|
|
|
|
|
|
|
length([_| Tail], Acc, Length) :-
|
|
|
|
Acc2 is Acc + 1,
|
|
|
|
length(Tail, Acc2, Length).
|
|
|
|
|
|
|
|
|
|
|
|
max([N| Ns], Max) :-
|
|
|
|
max(Ns, N, Max).
|
|
|
|
|
|
|
|
max([], Max, Max).
|
|
|
|
|
|
|
|
max([N| Ns], Aux, Max) :-
|
2006-12-28 13:03:34 +00:00
|
|
|
( N @> Aux ->
|
2001-06-06 20:40:57 +01:00
|
|
|
max(Ns, N, Max)
|
2006-12-28 13:03:34 +00:00
|
|
|
; max(Ns, Aux, Max)
|
|
|
|
).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
member(Element, [Element| _]).
|
|
|
|
|
|
|
|
member(Element, [_| List]) :-
|
|
|
|
member(Element, List).
|
|
|
|
|
|
|
|
|
|
|
|
memberchk(Element, [Element| _]) :-
|
|
|
|
!.
|
|
|
|
|
|
|
|
memberchk(Element, [_| List]) :-
|
|
|
|
memberchk(Element, List).
|
|
|
|
|
|
|
|
|
|
|
|
min([N| Ns], Min) :-
|
|
|
|
min(Ns, N, Min).
|
|
|
|
|
|
|
|
min([], Min, Min).
|
|
|
|
|
|
|
|
min([N| Ns], Aux, Min) :-
|
2006-12-28 13:03:34 +00:00
|
|
|
( N @< Aux ->
|
2001-06-06 20:40:57 +01:00
|
|
|
min(Ns, N, Min)
|
2006-12-28 13:03:34 +00:00
|
|
|
; min(Ns, Aux, Min)
|
|
|
|
).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
new([]).
|
|
|
|
|
|
|
|
|
2004-06-06 23:46:45 +01:00
|
|
|
nextto(X, Y, [X, Y| _]).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
2004-06-06 23:46:45 +01:00
|
|
|
nextto(X, Y, [_| Tail]) :-
|
|
|
|
nextto(X, Y, Tail).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
2004-06-06 23:46:45 +01:00
|
|
|
nth0(Nth, List, Element) :-
|
|
|
|
nth(Element, List, 0, Nth, _).
|
|
|
|
|
|
|
|
|
|
|
|
nth0(Nth, List, Element, Tail) :-
|
|
|
|
nth(Element, List, 0, Nth, Tail).
|
|
|
|
|
|
|
|
|
|
|
|
nth1(Nth, List, Element) :-
|
|
|
|
nth(Element, List, 1, Nth, _).
|
|
|
|
|
|
|
|
|
|
|
|
nth1(Nth, List, Element, Tail) :-
|
|
|
|
nth(Element, List, 1, Nth, Tail).
|
|
|
|
|
|
|
|
|
2006-12-28 13:03:34 +00:00
|
|
|
nth(Element, List, Acc, Nth, Tail) :-
|
|
|
|
( integer(Nth),
|
|
|
|
Nth >= Acc,
|
|
|
|
nth_aux(NthElement, List, Acc, Nth, Tail) ->
|
|
|
|
Element = NthElement
|
|
|
|
; var(Nth),
|
|
|
|
nth_aux(Element, List, Acc, Nth, Tail)
|
|
|
|
).
|
2004-06-06 23:46:45 +01:00
|
|
|
|
2006-12-28 13:03:34 +00:00
|
|
|
|
|
|
|
nth_aux(Head, [Head| Tail], Position, Position, Tail).
|
|
|
|
|
|
|
|
nth_aux(Head, [_| List], Count, Position, Tail) :-
|
2001-06-06 20:40:57 +01:00
|
|
|
Count2 is Count + 1,
|
2006-12-28 13:03:34 +00:00
|
|
|
nth_aux(Head, List, Count2, Position, Tail).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
permutation(List, Permutation) :-
|
|
|
|
same_length(List, Permutation),
|
|
|
|
permutation2(List, Permutation).
|
|
|
|
|
|
|
|
|
|
|
|
permutation2([], []).
|
|
|
|
|
|
|
|
permutation2(List, [Head| Tail]) :-
|
|
|
|
select(Head, List, Remaining),
|
|
|
|
permutation2(Remaining, Tail).
|
|
|
|
|
|
|
|
|
|
|
|
prefix([], _).
|
|
|
|
|
|
|
|
prefix([Element| Tail], [Element| Tail2]) :-
|
|
|
|
prefix(Tail, Tail2).
|
|
|
|
|
|
|
|
|
|
|
|
reverse(List, Reversed) :-
|
2003-06-26 15:52:54 +01:00
|
|
|
reverse(List, [], Reversed, Reversed).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
2003-06-26 15:52:54 +01:00
|
|
|
reverse([], Reversed, Reversed, []).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
2003-06-26 15:52:54 +01:00
|
|
|
reverse([Head| Tail], List, Reversed, [_| Bound]) :-
|
|
|
|
reverse(Tail, [Head| List], Reversed, Bound).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
same_length([], []).
|
|
|
|
|
|
|
|
same_length([_| Tail1], [_| Tail2]) :-
|
|
|
|
same_length(Tail1, Tail2).
|
|
|
|
|
|
|
|
|
|
|
|
select(Head, [Head| Tail], Tail).
|
|
|
|
|
|
|
|
select(Head, [Head2| Tail], [Head2| Tail2]) :-
|
|
|
|
select(Head, Tail, Tail2).
|
|
|
|
|
|
|
|
|
|
|
|
sort(List, Sorted) :-
|
|
|
|
{sort(List, Sorted)}.
|
|
|
|
|
|
|
|
|
|
|
|
sublist(List, List).
|
|
|
|
|
|
|
|
sublist(Sublist, [Head| Tail]):-
|
|
|
|
sublist(Tail, Head, Sublist).
|
|
|
|
|
|
|
|
|
|
|
|
sublist(Sublist, _, Sublist).
|
|
|
|
|
|
|
|
sublist([Head| Tail], _, Sublist):-
|
|
|
|
sublist(Tail, Head, Sublist).
|
|
|
|
|
|
|
|
sublist([Head| Tail], Element, [Element| Sublist]):-
|
|
|
|
sublist(Tail, Head, Sublist).
|
|
|
|
|
|
|
|
|
|
|
|
subtract([], _, []).
|
|
|
|
|
|
|
|
subtract([Head| Tail], List, Rest) :-
|
2006-12-28 13:03:34 +00:00
|
|
|
( ::memberchk(Head, List) ->
|
2001-06-06 20:40:57 +01:00
|
|
|
subtract(Tail, List, Rest)
|
2006-12-28 13:03:34 +00:00
|
|
|
; Rest = [Head| Tail2],
|
|
|
|
subtract(Tail, List, Tail2)
|
|
|
|
).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
suffix(List, List).
|
|
|
|
|
|
|
|
suffix(List, [_| Tail]) :-
|
|
|
|
suffix(List, Tail).
|
|
|
|
|
|
|
|
|
|
|
|
valid(List) :-
|
|
|
|
nonvar(List),
|
|
|
|
\+ \+ valid2(List).
|
|
|
|
|
|
|
|
|
|
|
|
valid2([]).
|
|
|
|
|
|
|
|
valid2([_| List]) :-
|
|
|
|
valid2(List).
|
|
|
|
|
|
|
|
|
|
|
|
:- end_object.
|