Skip to content

Instantly share code, notes, and snippets.

View trietptm's full-sized avatar
💭
Information Security Consulting, Threat Hunting

Minh-Triet Pham Tran trietptm

💭
Information Security Consulting, Threat Hunting
View GitHub Profile
@trietptm
trietptm / gist:4257518
Created December 11, 2012 10:08
Reverse a list.
appendlist([], X, [X]).
appendlist([H|T], X, [H|T1]) :- appendlist(T, X, T1).
reverse_list([], []).
reverse_list([X], [X]).
reverse_list([X|T1], L) :- reverse_list(T1, T2), appendlist(T2, X, L),!.
@trietptm
trietptm / gist:4258577
Created December 11, 2012 13:30
Find out whether a list is a palindrome.
appendlist([], X, [X]).
appendlist([H|T], X, [H|T1]) :- appendlist(T, X, T1).
palindrome([]).
palindrome([X]).
palindrome([X|T]) :- appendlist(L, X, T), palindrome(L).
@trietptm
trietptm / gist:4259176
Created December 11, 2012 15:05
Fibonacci
fib(0, 0).
fib(1, 1).
fib(X, Y) :- X>1, X1 is X - 1, X2 is X - 2, fib(X1, Y1), fib(X2, Y2), Y is Y1 + Y2.
@trietptm
trietptm / gist:4277556
Created December 13, 2012 16:17
Eliminate consecutive duplicates of list elements. If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed.
compress([X|[]], [X]).
compress([X|[X|Z]], L) :- compress([X|Z], L),!.
compress([X|[Y|Z]], [X|L]) :- compress([Y|Z], L),!.
@trietptm
trietptm / gist:4277626
Created December 13, 2012 16:25
Duplicate the elements of a list.
dupli([], []).
dupli([X], [X, X]).
dupli([H|T], [H, H|L]) :- dupli(T, L), !.
@trietptm
trietptm / gist:4277978
Created December 13, 2012 17:03
Duplicate the elements of a list a given number of times. Example: ?- dupli([a,b,c],3,X). X = [a,a,a,b,b,b,c,c,c]
appendlist([], X, X).
appendlist([H|T], L1, [H|L2]) :- appendlist(T, L1, L2).
dupli([], M, []) :-!.
dupli([X], 1, [X]) :-!.
dupli([X], M, [X|L]) :- N is M - 1, dupli([X], N, L), !.
dupli([H|T], M, L1) :- dupli([H], M, H1), dupli(T, M, T1), appendlist(H1, T1, L1), !.
@trietptm
trietptm / gist:4278660
Created December 13, 2012 18:51
Split a list into two parts; the length of the first part is given. Do not use any predefined predicates. Example: ?- split([a,b,c,d,e,f,g,h,i,k],3,L1,L2). L1 = [a,b,c] L2 = [d,e,f,g,h,i,k]
length_list([], 0).
length_list([H|T], X) :- length_list(T, X1), X is X1+1, !.
split_list([X|Y], 1, [X], Y).
split_list([H|T], M, [H|L1], L2) :- split_list(T, M1, L1, L2), M is M1+1.
@trietptm
trietptm / gist:4284788
Created December 14, 2012 11:34
Factorial
fact(0, 1).
fact(X, Y) :- X>0, X1 is X-1, fact(X1, Y1), Y is X*Y1.
@trietptm
trietptm / gist:4286485
Created December 14, 2012 16:01
Sum of digits of a number (in PROLOG) a simple snippet to find the sum of digits of a given number in PROLOG
sumofdigits(X, X) :- X<10.
sumofdigits(X, Y) :- X>=10, X1 is X // 10, X2 is X mod 10, sumofdigits(X1, Y1), Y is Y1 + X2.
@trietptm
trietptm / gist:4286587
Created December 14, 2012 16:13
Tìm phần tử bé nhất của 1 list bằng Prolog
findmin([X], X).
findmin([H|T], X) :- findmin(T, X), H >= X, !.
findmin([H|T], H) :- findmin(T, X), H < X, !.