Skip to content

Commit

Permalink
fixes #807 (#1013)
Browse files Browse the repository at this point in the history
* fixes #807

* no eval here
  • Loading branch information
martin-henz authored Jul 3, 2024
1 parent 245bdc0 commit b3395b7
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions xml/chapter2/section5/subsection3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,37 @@ head(tail(tail(tail(mul(p1, p2)))));
subtraction of polynomials.
(Hint: You may find it helpful to define a generic negation operation.)
<LABEL NAME="ex:sub-poly"/>
<SOLUTION>
<SNIPPET EVAL="no">
<NAME>ex_2_88_solution</NAME>
<JAVASCRIPT>
function sub_terms(L1, L2) {
if (is_empty_termlist(L1)) {
return L2;
} else if (is_empty_termlist(L2)) {
return L1;
} else {
const t1 = first_term(L1);
const t2 = first_term(L2);
return order(t1) &gt; order(t2)
? adjoin_term(t1, sub_terms(rest_terms(L1), L2))
: order(t1) &lt; order(t2)
? adjoin_term(t2, sub_terms(L1, rest_terms(L2)))
: adjoin_term(make_term(order(t1),
sub(coeff(t1), coeff(t2))),
sub_terms(rest_terms(L1),
rest_terms(L2)));
}
}
function sub_poly(p1, p2) {
return is_same_variable(variable(p1), variable(p2))
? make_poly(variable(p1),
sub_terms(term_list(p1), term_list(p2)))
: error(list(p1, p2), "polys not in same var -- sub_poly");
}
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>
</EXERCISE>

<EXERCISE>
Expand Down

0 comments on commit b3395b7

Please sign in to comment.