0
$\begingroup$

I have a question regarding pattern matching. Can anyone explain the results i get ?

Cases[Table[x^n, {n, 1, 10}], a_^1] 

Which gives the following result.

{x, x^2, x^3, x^4, x^5, x^6, x^7, x^8, x^9, x^10}

I was expecting it to return x only similar to the case of x^2 shown below

Cases[Table[x^n, {n, 1, 10}], a_^2]

{x^2}

Thanks David.

$\endgroup$
2
$\begingroup$

a_^1 is converted to a_ before Cases is executed, matching each element of the Table.

Trace[Cases[Table[x^n, {n, 1, 10}], a_^1]]

(The output is a bit long to be presented here.)

$\endgroup$
0
$\begingroup$

Most Likely is because x^n^1 == x^n

x^n == (x^n)^1
(*True*)

a_ can be anything including x^n

If you try for example

Cases[Table[x^n, {n, 1, 10}], Power[x,1]] 
(*{x}*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.