-
Notifications
You must be signed in to change notification settings - Fork 0
/
prolude.pl
185 lines (154 loc) · 3.42 KB
/
prolude.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
% Max Nordlund
% A Matrix is a List of Lists
:- module(prolude, [
map/3, % :Goal, +InputList, -OutputList
filter/3, % :Goal, +InputList, -OutputList
any/2, % :Goal, +List
all/2, % :Goal, +List
not/2, % :Goal, ?Input
head/2, % +List, -Head
last/2, % +List, -Last
tail/2, % +InputList, -OutputList
init/2, % +InputList, -OutputList
null/1, % ?List
in/2, % ?List, ?Element
foldl/3, % :Goal, +List, -Result
foldr/3, % :Goal, +List, -Result
concat/2, % +Matrix, -List
reverse/2, % +InputList, -OutputList
trans/2, % +InputMatrix, -OutputMatrix
splitBy/3, % +Delimiter, +String, -ListOfStrings
add/3, % +A, +B, -Result
sub/3, % +A, +B, -Result
mul/3, % +A, +B, -Result
div/3, % +A, +B, -Result
lt/2, % +A, +B
le/2, % +A, +B
gt/2, % +A, +B
ge/2, % +A, +B
eq/2, % +A, +B
ne/2 % +A, +B
]).
:- meta_predicate map(2, ?, ?).
:- meta_predicate filter(1, ?, ?).
:- meta_predicate any(1, +).
:- meta_predicate all(1, +).
:- meta_predicate not(1, ?).
:- meta_predicate foldl(3, +, -).
:- meta_predicate foldr(3, +, -).
map(G, L, R) :-
_map(G, L, [], R).
_map(_, [], R, R).
_map(G, [H|T], B, R) :-
call(G, H, A),
_map(G, T, [A|B], R).
filter(G, L, R) :-
_filter(G, L, [], R).
_filter(_, [], R, R).
_filter(G, [H|T], B, R) :-
(call(G, H) ->
_filter(G, T, [H|B], R)
;
_filter(G, T, B, R)
).
any(_, []).
any(G, [H|T]) :-
call(G, H);
any(T).
all(_, []).
all(G, [H|T]) :-
call(G, H),
all(G, T).
not(G, I) :-
\+ call(G, I).
head([H|_], H).
last([R|[]], R).
last([_|T], R) :-
last(T, R).
tail([_|T], T).
init(L, R) :-
_init(L, [], R).
_init([], [_|L], R) :-
reverse(L, R).
_init([H|T], L, R) :-
_init(T, [H|L], R).
null([]).
in(L, E) :-
member(E, L).
foldl(G, L, R) :-
_foldl(G, L, _, R).
_foldl(_, [], R, R).
_foldl(G, [H|T], A, R) :-
call(G, A, H, B),
_foldl(G, T, B, R).
foldr(G, L, R) :-
_foldr(G, L, _, R).
_foldr(_, [], R, R).
_foldr(G, [H|T], A, R) :-
_foldr(G, T, A, B),
call(G, B, H, R).
colon(A, B, [B|A]).
prepend([], R, R).
prepend([H|T], L, R) :-
prepend(T, [H|L], R).
reverse(L, R) :-
prepend(L, [], R).
%% _reverse(L, [], R).
%% _reverse([], R, R).
%% _reverse([H|T], L, R) :-
%% _reverse(T, [H|L], R).
concat(L, R) :-
_concat(L, [], A),
reverse(A, R).
_concat([], R, R).
_concat([H|T], A, R) :-
prepend(H, A, B),
_concat(T, B, R).
transpose(M, R) :-
map()
transpose(L, R) :-
_transpose(L, [], [], [], A),
map(reverse, A, R).
_transpose([], [], Bc, Bm, [Bc|Bm]).
_transpose([], Rest, Bc, Bm, R) :-
_transpose(Rest, [], [], [Bc|Bm], R).
_transpose([[H|[]]|T], Rest, Bc, Bm, R) :-
_transpose(T, [], [H|Bc], Bm, R).
_transpose([[H|Tr]|T], Rest, Bc, Bm, R) :-
append(Rest, [Tr], A)
_transpose(T, A, [H|Bc], Bm, R).
splitBy(Sp, Str, R) :-
_splitBy(Sp, Str, [], [], A),
filter(not(null), A, B),
map(reverse, L, A),
reverse(A, R).
_splitBy(_, [], Bs, B, [Bs|B]).
_splitBy(Sp, [H|T], Bsi, Bi, R) :-
(member(H, Sp) ->
(Bso = [],
Bo = [Bsi|Bi])
;
(Bso = [H|Bsi],
Bo = Bi)
),
_splitBy(Sp, T, Bso, Bo, R).
lt(A, B) :-
A < B.
le(A, B) :-
A =< B.
gt(A, B) :-
A > B.
ge(A, B) :-
A >= B.
eq(A, B) :-
A == B.
ne(A, B) :-
A \== B.
add(A, B, C) :-
C is A + B.
sub(A, B, C) :-
C is A - B.
mul(A, B, C) :-
C is A * B.
div(A, B, C) :-
C is A / B.