83 lines
1.7 KiB
Prolog
83 lines
1.7 KiB
Prolog
:- [polymani].
|
|
|
|
% numeros
|
|
num('zero', 0).
|
|
num('one', 1).
|
|
num('two', 2).
|
|
num('three', 3).
|
|
num('four', 4).
|
|
num('five', 5).
|
|
num('six', 6).
|
|
num('seven', 7).
|
|
num('eight', 8).
|
|
num('nine', 9).
|
|
num('ten', 10).
|
|
|
|
% operações
|
|
get_num(X, Z) :- num(X, Z), !.
|
|
get_num(X, X).
|
|
|
|
parse_numbers([H|T], [N|U]) :- get_num(H, N), parse_numbers(T, U).
|
|
parse_numbers([], []).
|
|
|
|
% --
|
|
parse_power([X, raised, to, Y | T], [K|NewT]) :-
|
|
number(X),
|
|
number(Y),
|
|
K is X ** Y,
|
|
parse_power(T, NewT),
|
|
!.
|
|
parse_power([X, raised, to, Y | T], [X^Y|NewT]) :-
|
|
number(X),
|
|
parse_power(T, NewT),
|
|
!.
|
|
parse_power([X, raised, to, Y | T], Z) :-
|
|
poly2list(X^Y, K),
|
|
parse_power(T, NewT),
|
|
append(K, NewT, Z),
|
|
!.
|
|
parse_power([X, squared | T], Z) :-
|
|
number(X),
|
|
A is X**2,
|
|
parse_power([A|T], Z),
|
|
!.
|
|
parse_power([x, squared | T], [x^2|Z]) :-
|
|
parse_power(T, Z),
|
|
!.
|
|
parse_power([H|T], [H|Z]) :-
|
|
parse_power(T, Z).
|
|
parse_power([], []).
|
|
|
|
% --
|
|
parse_multiplication([X, times, Y | T], Z) :-
|
|
simpoly(X*Y, A),
|
|
parse_multiplication([A|T], Z),
|
|
!.
|
|
parse_multiplication([multiply, X, by, Y | T], Z) :-
|
|
simpoly(X*Y, A),
|
|
parse_multiplication([A|T], Z),
|
|
!.
|
|
parse_multiplication([H|T], [H|Z]) :-
|
|
parse_multiplication(T, Z).
|
|
parse_multiplication([], []).
|
|
|
|
% --
|
|
parse_sum([X, plus, Y|T], Z) :-
|
|
simpoly(X+Y, A),
|
|
parse_sum([A|T], Z),
|
|
!.
|
|
parse_sum([H|T], [H|Z]) :-
|
|
parse_sum(T, Z).
|
|
parse_sum([], []).
|
|
|
|
|
|
parse(L, Z) :- parse_numbers(L, A), parse_power(A, B), parse_multiplication(B, C), parse_sum(C, Z).
|
|
|
|
%% tests
|
|
% parse([two, plus, x, squared], A).
|
|
% A = [x^2+2].
|
|
% parse([multiply, three, by, two, plus, x, squared], B).
|
|
% B = [x^2+6].
|
|
% parse([two, plus, two, plus, one, times, y], C).
|
|
% C = [1*y+4].
|