Using Patterns to check for correctness

Can any of you identify why exp3 and exp5 are not giving me correct results but exp1 and exp2 are.

And I’m not getting any syntax errors.

I’m using these pattern definitions as a second part of determining if the expression inputted is correct. The first part is with simplefunction. I call these definitions later by checking exp.matches().

I’m very unsure about my use of patterns.literal, by the way.

It’ll be really helpful if you share your activity so we can check out your overall code (there may be a simpler solution than pattern matching). Also, we have to assume what solution you’re looking for just based on the code.

I believe literal is exactly what you type so expr3 is looking for 8*a*b*7. (I’m not entirely sure if p.product distinguishes between different orders.)

https://teacher.desmos.com/activitybuilder/custom/613668a6e25737095d0bc844

For expr3, I was actually looking for 8a(b-7) to be the correct response. And I wasn’t too concerned about order.

Sorry, I misread. Looks like what it’s supposed to be, but I’ll have to look into your activity if someone doesn’t get to it first.

I created another activity requiring checking of factors. It allowed me to check this one: m²(9m+1)(9m-1), but not this one: 4x(2x+5)(2x-5). So, I wonder if it’s limiting the product to 3 items… Nope, just edited this to say, it seems like it doesn’t like having the literal as part of the product outside of the parentheses.

Maybe the latter example is considered a product of 4 items because of 4 and the x.

But I should have been able to do 8a(b-7) because that is just 3. When I put product(product()), it told me not to embed. I’ve resorted to countNumberUsage, for now. Thank you!

Repeated patterns can probably help you out here and will probably give you some clues as to why its not parsing the way you expect: Repeated Patterns • Activity Builder by Desmos

Maybe use an extra parameter instead of product within product:

product(p.integer, p.literal(`x`), p.expression, p.expression)

I wonder if structuring it where you use a multivaroable function to confirm it’s equivalent to the original but use pattern match in addition more generically (i.e product of 3 expressions) would be a better approach.

Not sure if it’d introduce any false positives/negatives.

Yes, that or using a lighter touch with the pattern matching and then simpleFunction to validate individual terms would be my approach.