@@ -1340,7 +1340,7 @@ list("x", "+", list(3, "*", list("x", "+", list("y", "+", 2))))
1340
1340
example:
1341
1341
<SNIPPET EVAL =" no" >
1342
1342
<JAVASCRIPT >
1343
- list("x", "+", "3" , "*", list("x", "+", "y", "+", 2))
1343
+ list("x", "+", 3 , "*", list("x", "+", "y", "+", 2))
1344
1344
</JAVASCRIPT >
1345
1345
</SNIPPET >
1346
1346
</JAVASCRIPT >
@@ -1436,15 +1436,27 @@ deriv(list("x", "*", 4), "x");
1436
1436
<EXAMPLE >example_2.61_2</EXAMPLE >
1437
1437
<JAVASCRIPT >
1438
1438
function items_before_first(op, s) {
1439
- return head(s) === op
1439
+ return is_string(head(s)) && head(s) === op
1440
1440
? null
1441
1441
: pair(head(s),
1442
1442
items_before_first(op, tail(s)));
1443
1443
}
1444
1444
function items_after_first(op, s) {
1445
- return head(s) === op
1445
+ return is_string(head(s)) && head(s) === op
1446
1446
? tail(s)
1447
- : items_after_first(op, tail(s);
1447
+ : items_after_first(op, tail(s));
1448
+ }
1449
+ function simplify_unary_list(s) {
1450
+ return is_pair(s) && is_null(tail(s))
1451
+ ? head(s)
1452
+ : s;
1453
+ }
1454
+ function contains_plus(s) {
1455
+ return is_null(s)
1456
+ ? false
1457
+ : is_string(head(s)) && head(s) === "+"
1458
+ ? true
1459
+ : contains_plus(tail(s));
1448
1460
}
1449
1461
function make_sum(a1, a2) {
1450
1462
return number_equal(a1, 0)
@@ -1458,14 +1470,13 @@ function make_sum(a1, a2) {
1458
1470
// a sequence of terms and operators is a sum
1459
1471
// if and only if at least one + operator occurs
1460
1472
function is_sum(x) {
1461
- return is_pair(x) &&
1462
- ! (is_null(member("+", x));
1473
+ return is_pair(x) && contains_plus(x);
1463
1474
}
1464
1475
function addend(s) {
1465
- return items_before_first("+", s);
1476
+ return simplify_unary_list( items_before_first("+", s) );
1466
1477
}
1467
1478
function augend(s) {
1468
- return items_after_first("+", s);
1479
+ return simplify_unary_list( items_after_first("+", s) );
1469
1480
}
1470
1481
function make_product(m1, m2) {
1471
1482
return number_equal(m1, 0) || number_equal(m2, 0)
@@ -1481,13 +1492,13 @@ function make_product(m1, m2) {
1481
1492
// a sequence of terms and operators is a product
1482
1493
// if and only if no + operator occurs
1483
1494
function is_product(x) {
1484
- return is_pair(x) && is_null(member("+", x);
1495
+ return is_pair(x) && ! contains_plus( x);
1485
1496
}
1486
1497
function multiplier(s) {
1487
- return items_before_first("*", s);
1498
+ return simplify_unary_list( items_before_first("*", s) );
1488
1499
}
1489
1500
function multiplicand(s) {
1490
- return items_after_first("*", s);
1501
+ return simplify_unary_list( items_after_first("*", s) );
1491
1502
}
1492
1503
function deriv(exp, variable) {
1493
1504
return is_number(exp)
0 commit comments