3
$\begingroup$

Multiple assignment is supported by Set. How can do something similar in Module expressions? If I could do it, whould there be drawbacks?

Here is what I tried so far:

g[x11_, y11_] := {xx111, yy111} = {x11, y11}

g[x11_, y11_] := Module[{{x111, y111} = {x11, y11}}, 1]

g[1, 2]

Module::lvset: Local variable specification {{x111, y111} = {1, 2}} contains {x111, y111} = {1, 2}, which is an assignment to {x111,y111}; only assignments to symbols are allowed. >>

Possible alternatives are:

ggg[x11_, y11_] := 
  Module[{xx = {x11, y11}, x1, x2}, {x1 = xx[[2]], x2 = xx[[1]]}]

or

ggg[x11_, y11_] := Module[{x1111=x11, y1111=y11}, what... ]
$\endgroup$
2
  • $\begingroup$ "only assignments to symbols are allowed" - that's right, the scoping constructs don't allow parallel assignment in the variable list. $\endgroup$ Commented May 24, 2013 at 3:23
  • $\begingroup$ In my answer to a similar question about With on SO, I posted a macro which can do this. You can just replace With with Module there, if that kind of solution fits you. $\endgroup$ Commented May 24, 2013 at 11:14

1 Answer 1

4
$\begingroup$

As J.M. points out in his comment, the kind of multiple assignment you want is simply not supported. The closest you can get (and it is perfectly serviceable in my opinion) is

g[x11_, y11_] := Module[{x111, y111},
  {x111, y111} = {x11, y11};
  1]
$\endgroup$
2
  • $\begingroup$ en, seems declare variables and then assign separately. Anyway, this is not big problem, I just take some habits from one function elsewhere. $\endgroup$ Commented May 24, 2013 at 4:07
  • $\begingroup$ @Hyper, yes, that's precisely what m_goldberg does. That's how it is... $\endgroup$ Commented May 24, 2013 at 8:24

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.