Skip to content

Instantly share code, notes, and snippets.

@Lukas0025
Created June 1, 2021 13:13
Show Gist options
  • Save Lukas0025/d0adef8e2c290507bd12aaee00ddce85 to your computer and use it in GitHub Desktop.
Save Lukas0025/d0adef8e2c290507bd12aaee00ddce85 to your computer and use it in GitHub Desktop.
Prolog basics
% Basics in Prolog language
% @autor Lukáš Plevač <xpleva07@vutbr.cz>
% @date 1.6.2021
% under CC0
% mod([list of numbers], mod by num, [res])
mod([], _, []).
mod([H|T], X, [G|L]) :- G is H mod X, mod(T, X, L).
%mod([H|T], X, [G|L]) :- ownmod(H, X, G), mod(T, X, L).
% is element in array
% in([X], X). explicid is in line under
in([X|_], X).
in([_|T], X) :- in(T,X).
% push item to top
push([], A, [A]).
push([H|T], A, [A, H|T]).
% append item to end
append([], A, [A]).
append([H|T], A, [H|L]) :- append(T, A, L).
% marge two array
marge([], [], []).
marge([], [H|T], [H|T]).
marge([H|T], [], [H|T]).
marge([H1|T1], [H2|T2], [H1, H2|T3]) :- marge(T1,T2,T3).
% own mod operation
ownmod(Num, Base, Res) :- Res is Num - (floor(Num / Base) * Base).
% same elements of arrays (conjuction)
same([], _, []).
same([H|T1], M, [H|T3]) :- member(H, M), same(T1, M, T3).
same([_|T1], M, M2) :- same(T1, M, M2).
% example calls
% mod([3,10], 2, Y)
%% Y=[1,0]
% in([3,10], 2)
%% false
% push([3,10], 2, Y)
%% Y=[2,3,10]
% append([3,10], 2, Y)
%% Y=[3,10,2]
% merge([3,10], [2], Y)
%% Y=[3,2,10]
% same([3,10], [3], Y)
%% Y=[3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment