-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmap.pro
More file actions
56 lines (44 loc) · 1.07 KB
/
Copy pathmap.pro
File metadata and controls
56 lines (44 loc) · 1.07 KB
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
:- module(map, [
map/5
, map/4
, map/3
, filter/4
]).
/*
Make assert work.
*/
:- meta_predicate
map(?, 0, ?)
, map(?, ?, ?, ?)
, map(?, ?, 0, ?, ?)
, filter(?, 0, ?, ?)
.
/** map(In, Out, Goal, Ins, Outs)
In should be a variable that occurs in Goal.
Out should be a variable that occurs in Goal.
In is repeatedly unified with the elements of Ins.
Example:
```
?- Xs = [1,2,3], map(X, Y, Y #= X + 1, Xs, Ys).
```
That produces Ys = [2,3,4].
Example:
```
f(X, Y) :- Y #= X + 1.
f(X, Y) :- Y #= X + 2.
?- Xs = [1,2,3], map(X, Y, f(X,Y), Xs, Ys).
```
That produces Ys = [2,3,3,4,4,5].
Grounding the In variable filters the output Outs.
Example:
```
f(X, Y) :- Y #= X + 1.
f(X, Y) :- Y #= X + 2.
?- Xs = [1,2,3], X #=< 2, map(X, Y, f(X,Y), Xs, Ys).
```
That produces Ys = [2,3,3,4].
*/
map(In, Out, Goal, Ins, Outs) :- findall(Out, (member(In, Ins), Goal), Outs).
map(In, Out, Ins, Outs) :- findall(Out, member(In, Ins), Outs).
map(In, Goal, Ins) :- forall(member(In, Ins), Goal).
filter(In, Goal, Ins, Outs) :- findall(In, (member(In, Ins), Goal), Outs).