-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.pl
79 lines (60 loc) · 2.12 KB
/
utils.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
:- module(utils, [
map/3,
add_tuple/3,
sum_tuples/2,
sum/2,
end/2,
last_phon/2,
max_tuples/2,
lowest_precision/2,
format_tuples/2,
make_symbol/2,
lookup_symbol/4
]).
map(_, [], []).
map(Pred, [X|Xs], [Y|Ys]) :- call(Pred, X, Y), map(Pred, Xs, Ys).
add_tuple(tuple(X1, X2), tuple(Y1, Y2), tuple(Z1, Z2)) :- Z1 is X1 + Y1, Z2 is X2 + Y2.
sum_tuples([], tuple(0, 0)).
sum_tuples([X|XS], Ret) :- sum_tuples(XS, Sum), add_tuple(X, Sum, Ret).
sum([], 0).
sum([X|XS], Ret) :- sum(XS, Sum), Ret is X + Sum.
end([], []).
end([X|[]], X) :- !.
end([_|XS], Ret) :- end(XS, Ret).
vowel(X) :- member(X, ['a', 'e', 'i', 'o', 'u']).
% Define the main function
last_phon(Atom, Sublist) :-
atom_chars(Atom, CharList),
reverse(CharList, Reversed),
extract_after_last_vowel(Reversed, Sublist).
% Define a helper predicate to extract the sublist after the last vowel
extract_after_last_vowel([], []).
extract_after_last_vowel([H|T], [H|Sublist]) :-
\+ vowel(H),
extract_after_last_vowel(T, Sublist).
extract_after_last_vowel([H|_], []) :-
vowel(H).
max_tuples([], []).
max_tuples([tuple(Name, Value) | Rest], MaxTuples) :-
max_tuples(Rest, RestMaxTuples),
max_value_in_tuples(RestMaxTuples, RestMaxValue),
(
Value > RestMaxValue, MaxTuples = [tuple(Name, Value)];
(
Value =:= RestMaxValue, MaxTuples = [tuple(Name, Value) | RestMaxTuples];
MaxTuples = RestMaxTuples
)
).
max_value_in_tuples([], 0).
max_value_in_tuples([tuple(_, Value) | Rest], MaxValue) :-
max_value_in_tuples(Rest, RestMaxValue),
MaxValue is max(Value, RestMaxValue).
lowest_precision([P|[]], P).
lowest_precision([approx|_], approx) :- !.
lowest_precision([exact|PS], P) :- lowest_precision(PS, P).
format_tuples([], []).
format_tuples([tuple(tuple(Name, Val), Val2) | Rest], [(Name, Val, Val2) | ResultRest]) :-
format_tuples(Rest, ResultRest).
make_symbol(_, Sym):- gensym(d, Sym).
lookup_symbol(Item, [Item|_], [Symbol|_], Symbol).
lookup_symbol(Item, [_|Arity_0], [_|Symbols], Symbol) :- lookup_symbol(Item, Arity_0, Symbols, Symbol).