1
$\begingroup$

I would like to understand the following module subtlety. Consider two seemingly equivalent instances

Module[{temp}, Sequence[x, x]] 

(Module::argrx: Module called with 3 arguments; 2 arguments are expected.)

and

Module[{temp}, temp = Sequence[x, x]]

(Sequence[x, x])

While the first complains about bad syntax (three arguments instead of expected two), the second performs as expected. Why the difference?

$\endgroup$
3
  • 1
    $\begingroup$ Module[{temp}, Sequence[x, x]] becomes Module[{temp}, x, x] hence the error. But Module[{temp}, temp = Sequence[x, x]] remains as is. Sequence[x, x] should become x,x in function calls. I admit I do not know why it unwrapped in the first usage you had instead of remaining Sequence[x, x] but this is what Mathematica did. You can see the automatic unwrapping if you type foo[Sequence[x, x]] then becomes foo[x,x] $\endgroup$
    – Nasser
    Commented Feb 2, 2022 at 8:15
  • $\begingroup$ @Nasser After reading your comment I started to think and now seems understand what is going here. Sequence starts to unwrap inside closest head. In the first case it is Module and this produces error. In the second case it tries to unwrap inside Set. However Set is very different form other command and it prevents unwrapping. Therefore the second case works as expected. $\endgroup$
    – Acus
    Commented Feb 2, 2022 at 8:37
  • 4
    $\begingroup$ The difference here is from the fact that Module has attribute HoldAll but not SequenceHold whereas Set has attribute SequenceHold. $\endgroup$ Commented Feb 2, 2022 at 15:50

0

Your Answer

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

Browse other questions tagged or ask your own question.