-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlists.pl
71 lines (54 loc) · 1.73 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
% Predicates working with Lists
write_english([]).
write_english([[City, england]| List]) :-
write(City),
nl,
write_english(List).
write_english([_|List]) :-
write_english(List).
find_largest([First|List], Maxval) :-
find_biggest(List, Maxval, First).
find_biggest([], LargestSoFar, LargestSoFar).
find_biggest([Head|Tail], Maxval, LargestSoFar) :-
Head > LargestSoFar,
find_biggest(Tail, Maxval, Head).
find_biggest([Head|Tail], Maxval, LargestSoFar) :-
Head =< LargestSoFar,
find_biggest(Tail, Maxval, LargestSoFar).
front([_], []).
front([Head|Tail], [Head|NewTail]) :- front(Tail, NewTail).
rear([], []).
rear([_|T], T).
inc([], []).
inc([H|T1], [Inc|T2]) :-
Inc is H + 1,
inc(T1, T2).
palindrome(List) :-
reverse(List, Rev),
Rev == List.
putfirst(Term, List, [Term|List]).
putlast(Term, [], [Term]) :- !.
putlast(Term, [H|T], [H|NewList]) :-
putlast(Term, T, NewList).
putlast2(Term, List, NewList) :-
append(List, [Term], NewList).
pred2([], []).
pred2([H|T],[[H]|NewT]) :-
pred2(T, NewT).
pred2find(Input, Output) :-
findall([Item], member(Item, Input), Output).
pred3([], []).
pred3([H|T], [pred(H, H)|NewT]) :-
pred3(T, NewT).
pred3find(Input, Output) :-
findall(pred(Item, Item), member(Item, Input), Output).
pred4(Input, Output) :-
findall([element, Item], member(Item, Input), Output).
% Define a last/2 predicate and a last_/3 help predicate that process a list
% to find its last element without leaving any choicepoints (query terminates)
% without needing to tip ";" and get the "false" reply from the toplevel
last([First|Rest], Last) :-
last_(Rest, First, Last).
last_([], Last, Last).
last_([First|Rest], _, Last) :-
last_(Rest, First, Last).