10
$\begingroup$

I feel that this must be a duplicate. But I just can't find such a post.

Mathematica has HoldFirst, HoldRest, HoldAll.

Why are these three "Hold"s sufficient? Why there is no HoldSecond, HoldThird, ... HoldN?

What if I want to hold several variables at different positions? For example how can I hold first and third at the same time?

$\endgroup$
5
  • 1
    $\begingroup$ Would HoldAll give you unwanted consequences in the case you describe? $\endgroup$
    – MarcoB
    Nov 2, 2015 at 16:38
  • 2
    $\begingroup$ Duplicate is on SO: Hold any argument $\endgroup$
    – jkuczm
    Nov 2, 2015 at 21:02
  • 1
    $\begingroup$ Any reason one couldn't do f[heldStuff_List, notHeld1_,...] with HoldFirst? $\endgroup$
    – N.J.Evans
    Nov 2, 2015 at 21:06
  • $\begingroup$ @N.J.Evans I drop a comment to m_goldberg, that also reply to your comment $\endgroup$
    – matheorem
    Nov 3, 2015 at 1:08
  • $\begingroup$ @MarcoB thank you, you are right, HoldAll works for the function that I am currently working with. $\endgroup$
    – matheorem
    Nov 3, 2015 at 1:23

1 Answer 1

5
$\begingroup$

There is no need for HoldSecond, etc., because arguments can always be reordered so the held argument is the first one. HoldRest is needed so an indefinite number of arguments can be passed, but the first argument subjected to pattern matching to discriminate which of many function definitions applies. HoldAll handles almost all other cases of non-standard evaluation, but see HoldAllComplete and SequenceHold.

The above may not be an exhaustive list of the reasons for these being the only holding patterns, but I believe it is sufficient to answer your question.

$\endgroup$
2
  • 4
    $\begingroup$ well, about reordering arguments, I have different opinion. I think we should order arguments in compliance with out logic, sometimes, reordering arguments makes me uncomfortable. :) $\endgroup$
    – matheorem
    Nov 3, 2015 at 1:06
  • $\begingroup$ My last comment has a mistake, it should be "our logic" not "out logic". But I found that I can't modify the comment any more. $\endgroup$
    – matheorem
    Nov 3, 2015 at 9:04

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.