1
$\begingroup$

Consider the following transformed function as an example:

FirstCase[Hold[Sqrt[-3(x+1)]-4],Sqrt[x_]:>x,x,∞]
FirstCase[HoldForm[Sqrt[-3(x+1)]-4],Sqrt[x_]:>x,x,∞]
FirstCase[HoldComplete[Sqrt[-3(x+1)]-4],Sqrt[x_]:>x,x,∞]
FirstCase[Unevaluated[Sqrt[-3(x+1)]-4],Sqrt[x_]:>x,x,∞]
FirstCase[Defer[Sqrt[-3(x+1)]-4],Sqrt[x_]:>x,x,∞]

None of them are able to grab the correct argument of Sqrt[...] which is -3(x+1).

$\endgroup$
0
1
$\begingroup$

The problem is that Sqrt[x_] gets nontrivially evaluated on the lhs of that rule—check Sqrt[x_] // FullForm. (Like :=, :> only prevents evaluation of its rhs, not its lhs.)

Instead, try using HoldPattern to prevent evaluation but maintain the same form for pattern matching:

FirstCase[Hold[Sqrt[-3 (x + 1)] - 4], HoldPattern[Sqrt[x_]] :> x, x, Infinity]
$\endgroup$
1
  • 1
    $\begingroup$ Thank you I didn't notice it as evaluating Sqrt[x_] still produced the nice typeset of Sqrt. $\endgroup$
    – user13892
    Jun 15 at 2:18

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.