Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

49
Task/Set/Prolog/set-1.pro Normal file
View file

@ -0,0 +1,49 @@
:- use_module(library(lists)).
set :-
A = [2, 4, 1, 3],
B = [5, 2, 3, 2],
( is_set(A) -> format('~w is a set~n', [A])
; format('~w is not a set~n', [A])),
( is_set(B) -> format('~w is a set~n', [B])
; format('~w is not a set~n', [B])),
% create a set from a list
list_to_set(B, BS),
( is_set(BS) -> format('~nCreate a set from a list~n~w is a set~n', [BS])
; format('~w is not a set~n', [BS])),
intersection(A, BS, I),
format('~n~w intersection ~w => ~w~n', [A, BS, I]),
union(A, BS, U),
format('~w union ~w => ~w~n', [A, BS, U]),
difference(A, BS, D),
format('~w difference ~w => ~w~n', [A, BS, D]),
X = [1,2],
( subset(X, A) -> format('~n~w is a subset of ~w~n', [X, A])
; format('~w is not a subset of ~w~n', [X, A])),
Y = [1,5],
( subset(Y, A) -> format('~w is a subset of ~w~n', [Y, A])
; format('~w is not a subset of ~w~n', [Y, A])),
Z = [1, 2, 3, 4],
( equal(Z, A) -> format('~n~w is equal to ~w~n', [Z, A])
; format('~w is not equal to ~w~n', [Z, A])),
T = [1, 2, 3],
( equal(T, A) -> format('~w is equal to ~w~n', [T, A])
; format('~w is not equal to ~w~n', [T, A])).
% compute difference of sets
difference(A, B, D) :-
exclude(member_(B), A, D).
member_(L, X) :-
member(X, L).
equal([], []).
equal([H1 | T1], B) :-
select(H1, B, B1),
equal(T1, B1).

55
Task/Set/Prolog/set-2.pro Normal file
View file

@ -0,0 +1,55 @@
%% Set creation
?- list_to_ord_set([1,2,3,4], A), list_to_ord_set([2,4,6,8], B).
A = [1, 2, 3, 4],
B = [2, 4, 6, 8].
%% Test m ∈ S -- "m is an element in set S"
?- ord_memberchk(2, $A).
true.
%% A B -- union; a set of all elements either in set A or in set B.
?- ord_union($A, $B, Union).
Union = [1, 2, 3, 4, 6, 8].
%% A ∩ B -- intersection; a set of all elements in both set A and set B.
?- ord_intersection($A, $B, Intersection).
Intersection = [2, 4].
%% A B -- difference; a set of all elements in set A, except those in set B.
?- ord_subtract($A, $B, Diff).
Diff = [1, 3].
%% A ⊆ B -- subset; true if every element in set A is also in set B.
?- ord_subset($A, $B).
false.
?- ord_subset([2,4], $B).
true.
%% A = B -- equality; true if every element of set A is in set B and vice-versa.
?- $A == $B.
false.
?- $A == [1,2,3,4].
true.
%% Definition of a proper subset:
ord_propsubset(A, B) :-
ord_subset(A, B),
\+(A == B).
%% add/remove elements
?- ord_add_element($A, 19, NewA).
NewA = [1, 2, 3, 4, 19].
?- ord_del_element($NewA, 3, NewerA).
NewerA = [1, 2, 4, 19].