-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path03-lists.pl
93 lines (68 loc) · 2.44 KB
/
03-lists.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
%-------------------------------------------------
% my_len/2 - find length of a list
my_len([],0).
my_len([_|T],F) :-
my_len(T,F1),
F is F1+1.
%-------------------------------------------------
% my_concat/3 - concatenate two lists
my_concat([], L2, L2).
my_concat([Elmt | L1], L2, [Elmt | C]) :-
my_concat(L1, L2, C).
%-------------------------------------------------
% my_last/2 - find last element of a list
my_last([X], X).
my_last([_|T],X) :- my_last(T,X).
%-------------------------------------------------
% my_nth/3 - find nth element of a list
my_nth(1, [H|_], H).
my_nth(N, [_|T], F) :-
N1 is N-1,
my_nth(N1, T, F).
%-------------------------------------------------
% my_rev/3 - find reverse of a list
my_rev(X,F) :- rev_helper(X,[],F).
rev_helper([],A,A).
rev_helper([H|T],A,F) :-
rev_helper(T,[H|A],F).
%-------------------------------------------------
% palindrome/1 - find whether list is a palindrome
palindrome(L) :- reverse(L, L).
%-------------------------------------------------
% my_takeout/3 - take out one occurrence of element E from list
% starts with 1st occurence of E in list
% backtracking removes 2nd occurence of E, then 3rd, etc.
my_takeout(E, [E|T], T).
my_takeout(E, [H|T], [H|T2]) :-
my_takeout(E, T, T2).
%-------------------------------------------------
% my_takeout2/3 - take out one occurrence of element E from list
% starts with last occurence of E in list
% backtracking removes next to last occurence of E, etc.
my_takeout2(E, [H|T], [H|T2]) :-
my_takeout2(E, T, T2).
my_takeout2(E, [E|T], T).
%-------------------------------------------------
% my_takeout_first/3 - take out only first occurrence of element from list
my_takeout_first(E, [E|T], T).
my_takeout_first(E, [H|T], [H|T2]) :-
E \= H,
my_takeout_first(E, T, T2).
% alternative implemention using cut
my_takeout_first2(E, [E|T], T) :- !.
my_takeout_first2(E, [H|T], [H|T2]) :-
my_takeout_first2(E, T, T2).
%-------------------------------------------------
% my_takeout_all/3 - take out all occurrences of element from list
my_takeout_all(_, [], []).
my_takeout_all(E, [E|T], T2) :-
my_takeout_all(E, T, T2).
my_takeout_all(E, [H|T], [H|T2]) :-
E \= H,
my_takeout_all(E, T, T2).
%-------------------------------------------------
% my_permutation/2 - find permutation of a list
my_permutation([], []).
my_permutation(L, [H|T]) :-
my_takeout(H, L, R),
my_permutation(R, T).