# Pattern matching sums with positive and negative terms

**URL:** <https://cl.desmos.com/t/pattern-matching-sums-with-positive-and-negative-terms/8354>\
**Category:** Questions\
**Tags:** patterns\
**Created:** [November 2, 2025, 4:56am UTC](https://cl.desmos.com/t/pattern-matching-sums-with-positive-and-negative-terms/8354 "2025-11-02T04:56:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![H\_Kiamil](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/h_kiamil/32/7988_2.png) [@H\_Kiamil](https://cl.desmos.com/u/H_Kiamil)\
**Post date:** [November 2, 2025, 4:56am UTC](https://cl.desmos.com/t/pattern-matching-sums-with-positive-and-negative-terms/8354/1 "2025-11-02T04:56:43Z")

</div>

Hello,

I am stuck trying to pattern match to sums with positive and negative terms. This is my activity so far: [Changing the Subject • Activity by H Kiamil](https://classroom.amplify.com/activity/6906bff84cdffaee6b34884f)

Could someone help me with page 6 (titled Question 5) please? The code currently accepts 7-2m+4d for the numerator, but I haven’t been able to get it to also accept 7+4d-2m. It would be even better if it also accepted variations where 7 appears later eg 4d+7-2m etc as well.

Thank you!

Hande

---

<div class="post-metadata">

**Author:** ![Daniel\_Grubbs](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/daniel_grubbs/32/719_2.png) [@Daniel\_Grubbs](https://cl.desmos.com/u/Daniel_Grubbs)\
**Post date:** [November 3, 2025, 3:52pm UTC](https://cl.desmos.com/t/pattern-matching-sums-with-positive-and-negative-terms/8354/2 "2025-11-03T15:52:51Z")

</div>

Honestly, unless you really want a particular form here, I think an evaluative check like below is sufficient. You could add some more evaluations to make it a little more robust. Trying to make your pattern check too specific can add significant complexity.

```auto
exp = this.latex
rhs = parseEquation(exp).rhs
expected = `\frac{7-2m+4d}{3}`

fnDiffCheck = simpleFunction(`\abs( ${expected} -(${rhs}) )`, `m`, `d`)
tolerance = 0.01
evalCheck = 
       fnDiffCheck.evaluateAt(3,4) < tolerance 
  and fnDiffCheck.evaluateAt(-3,2) < tolerance
  
check = evalCheck

```

One of the benefits of comparing evaluations is that you don’t have to code additional patterns to match. For example if each term was written as a fraction over 3, that’s a whole new pattern you have to deal with.
