-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfp.pl
46 lines (38 loc) · 901 Bytes
/
fp.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
:- module(fp, [ filter/3
, flip/3
, zipWith/4
, take1/2
, sublist/2
, tuple/3
, fliple/3
, fst/2
, snd/2
]).
:- meta_predicate filter(1, ?, ?).
:- meta_predicate flip(2, ?, ?).
:- meta_predicate zipWith(3, ?, ?, ?).
:- meta_predicate takeWhile(1, ?, ?).
flip(G_2, Y, Z) :-
call(G_2, Z, Y).
zipWith(_, [], [], []).
zipWith(G_3, [X|Xs], [Y|Ys], [Z|Zs]) :-
zipWith(G_3, Xs, Ys, Zs),
call(G_3, X, Y, Z).
sublist([], []).
sublist([_|T0], T1) :-
sublist(T0, T1).
sublist([H|T0], [H|T1]) :-
sublist(T0, T1).
take1([X|_], [X]).
take1([], []).
filter(_, [], []).
filter(G_1, [X|Xs], ZZs) :-
filter(G_1, Xs, Zs),
( call(G_1, X)
-> ZZs = [X|Zs]
; ZZs = Zs
).
tuple(X, Y, X-Y).
fliple(Y, X, X-Y).
fst(X-_, X).
snd(_-X, X).