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
| poly(Exp, L) :-
atomic_list_concat(List, ' ', Exp),
atomic_list_concat(List, Exp1),
atom_chars(Exp1, Chars),
phrase(extract(1, Chars), L).
extract(Mult, Exp) -->
extract_monome(Mult, Exp, Rest),
extract_rest(Rest).
extract_rest(['+' | T]) -->
extract(1, T).
extract_rest(['-' | T]) -->
extract(-1, T).
extract_rest([]) -->
[].
extract_monome(Mult, Exp, Rest) -->
extract_coef(Exp, Val, R1),
extract_variable(R1, R2),
{ Coef is Val * Mult},
extract_puissance(R2, Coef, Rest).
extract_coef([H | T], N, R) -->
{char_type(H, digit(V))},
extract_number(T, V, N, R).
extract_coef([H | T], N, R) -->
{\+char_type(H, digit),
N = 1, R = [H | T]},
[].
extract_number([H | T], V1, N, R) -->
{char_type(H, digit(V2)), V is V1 * 10 + V2},
extract_number(T, V, N, R).
extract_number([H | T], V1, V1, [H | T]) -->
{\+char_type(H, digit)},
[].
extract_number([], V1, V1, []) -->
[].
extract_variable([_, '^'|R], R) --> [].
extract_variable([], []) --> [].
extract_puissance([H | T], Coef, R) -->
{char_type(H, digit(V))},
extract_number(T, V, N, R),
[[Coef, N]].
extract_puissance([H | T], Coef, [H | T]) -->
{\+char_type(H, digit)},
[[Coef, 0]].
extract_puissance([], Coef, []) -->
[[Coef, 0]]. |
Partager