11
$\begingroup$

I would like to clear all definitions of the form f[1,...] in such a way that f[2,...] remain unaffected.

The two methods I know to clear variables don't seem to apply to this situation:

  1. Pattern matching with Clear:

    Clear[f[1,"*"]] or Clear["f[1,*]"]

  2. Using the command =.

    f[1,"*"]=.

The second option works for particular values of f[1,...] but doesn't work with pattern matching.

$\endgroup$
1

1 Answer 1

16
$\begingroup$

You can modify DownValues[f] directly. For example

DownValues[f] = 
 DeleteCases[DownValues[f], HoldPattern[_[f[1, ___]]] :> _]
$\endgroup$
4
  • $\begingroup$ This works, but I'm a bit confused about the syntax _[...]. Could you explain why the f[1,___] needs brackets around it in this context? $\endgroup$
    – WillG
    May 18, 2020 at 3:59
  • $\begingroup$ @WillG we need to match the HoldPattern as it appears in the DownValues, but without putting it explicitly in the pattern provided to DeleteCase. So we use a Blank. In other words, _[...] matches HoldPattern[...] but doesn't act as a held pattern. It looks confusing because then we have to wrap the pattern in HoldPattern to avoid evaluating the pattern. $\endgroup$ May 18, 2020 at 19:43
  • $\begingroup$ Ok I think I see. So the HoldPattern that appears explicitly in HoldPattern[_[f[1,___]]] is not what gets matched against the HoldPattern stored in DownValues? $\endgroup$
    – WillG
    May 18, 2020 at 21:42
  • $\begingroup$ @WillG exactly. $\endgroup$ May 19, 2020 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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