-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlists.pl
137 lines (106 loc) · 2.72 KB
/
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
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
/*
NOTE: a list is divided into head and tail
Head|Tail
H|T
*/
/*
lists are defined inductively:
- the atom [] (nil) is the empty list
- the compound term .(H, T) is a list iff T is a list
- '.'/2 is called list constructor
*/
:- use_module(library(lists)).
% represent strings as lists of characters
:- set_prolog_flag(double_quotes, chars).
% list/1, empty list
list([]).
% list/1, list of characters
list("hello world").
% list/1, list of integers
list([1, 2, 3, [4, 5]]).
?- list(X).
% X = [] ;
% X = [h, e, l, l, o, ' ', w, o, r|...] ;
% X = [1, 2, 3, [4, 5]].
% get head and tail
?- [H|T] = [1, 2, 3, [4, 5]].
% H = 1,
% T = [2, 3, [4, 5]].
% get head, ignore tail
?- [H|_] = [1, 2, 3, [4, 5]].
% H = 1.
% get specific elements
?- [_, _, _, [X, _]|_] = [1, 2, 3, [4, 5]].
% X = 4.
% add element to list
?- write([0|[1, 2, 3]]).
% [0,1,2,3]
% true.
% check if element in list
?- member(d, [a, b, c]). % false.
?- member(c, [a, b, c]). % true.
% iterate all elements in list
?- member(X, [a, b, c]).
% X = a ;
% X = b ;
% X = c.
% get element n in list
?- nth0(0, [1, 2, 3, [4, 5]], X). % X = 1.
?- nth0(1, [1, 2, 3, [4, 5]], X). % X = 2.
% last element in list
?- last([1, 2, 3, [4, 5]], X). % X = [4, 5].
% append
?- append([1, 2, 3], [4, 5], X). % X = [1, 2, 3, 4, 5].
?- append([1, 2, 3, 4, 5], [6], X). % X = [1, 2, 3, 4, 5, 6].
% delete
?- delete([1, 2, 3, 4, 5, 6], 6, X). % X = [1, 2, 3, 4, 5].
% length of list
?- length([1, 2, 3], X). % X = 3.
?- proper_length("hello world", X). % X = 11.
% reverse list
?- reverse("hello world", X). % X = [d, l, r, o, w, ' ', o, l, l|...].
% sort list
?- sort([2, 5, 1, 3, 4], X). % X = [1, 2, 3, 4, 5].
% permutations
?- permutation([a, b, c], X).
% X = [a, b, c] ;
% X = [a, c, b] ;
% X = [b, a, c] ;
% X = [b, c, a] ;
% X = [c, a, b] ;
% X = [c, b, a] ;
% false.
% flatten
?- flatten([1, 2, 3, [4, 5]], X). % X = [1, 2, 3, 4, 5].
% intersection
?- intersection([1, 2, 3], [3, 4, 5], X). % X = [3].
% union
?- union([1, 2, 3], [3, 4, 5], X). % X = [1, 2, 3, 4, 5].
/* EXAMPLE: recursive rule to iterate list */
% iterate_list/1
iterate_list([]).
iterate_list([Head|Tail]) :-
writeln(Head),
iterate_list(Tail).
?- X = "Michael", iterate_list(X).
% M
% i
% c
% h
% a
% e
% l
% false.
% increment_list/1
increment_list([]).
increment_list([Head|Tail], Result) :-
X is (Head + 1),
append(Result, [X], Y),
writeln(Y),
increment_list(Tail, Y).
?- increment_list([1, 2, 3, 4], []).
% [2]
% [2,3]
% [2,3,4]
% [2,3,4,5]
% false.