Skip to content

Commit b3395b7

Browse files
authored
fixes #807 (#1013)
* fixes #807 * no eval here
1 parent 245bdc0 commit b3395b7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

xml/chapter2/section5/subsection3.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,37 @@ head(tail(tail(tail(mul(p1, p2)))));
10251025
subtraction of polynomials.
10261026
(Hint: You may find it helpful to define a generic negation operation.)
10271027
<LABEL NAME="ex:sub-poly"/>
1028+
<SOLUTION>
1029+
<SNIPPET EVAL="no">
1030+
<NAME>ex_2_88_solution</NAME>
1031+
<JAVASCRIPT>
1032+
function sub_terms(L1, L2) {
1033+
if (is_empty_termlist(L1)) {
1034+
return L2;
1035+
} else if (is_empty_termlist(L2)) {
1036+
return L1;
1037+
} else {
1038+
const t1 = first_term(L1);
1039+
const t2 = first_term(L2);
1040+
return order(t1) &gt; order(t2)
1041+
? adjoin_term(t1, sub_terms(rest_terms(L1), L2))
1042+
: order(t1) &lt; order(t2)
1043+
? adjoin_term(t2, sub_terms(L1, rest_terms(L2)))
1044+
: adjoin_term(make_term(order(t1),
1045+
sub(coeff(t1), coeff(t2))),
1046+
sub_terms(rest_terms(L1),
1047+
rest_terms(L2)));
1048+
}
1049+
}
1050+
function sub_poly(p1, p2) {
1051+
return is_same_variable(variable(p1), variable(p2))
1052+
? make_poly(variable(p1),
1053+
sub_terms(term_list(p1), term_list(p2)))
1054+
: error(list(p1, p2), "polys not in same var -- sub_poly");
1055+
}
1056+
</JAVASCRIPT>
1057+
</SNIPPET>
1058+
</SOLUTION>
10281059
</EXERCISE>
10291060

10301061
<EXERCISE>

0 commit comments

Comments
 (0)