Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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... ]
share|improve this question
    
"only assignments to symbols are allowed" - that's right, the scoping constructs don't allow parallel assignment in the variable list. –  J. M. May 24 '13 at 3:23
    
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. –  Leonid Shifrin May 24 '13 at 11:14

1 Answer 1

up vote 4 down vote accepted

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]
share|improve this answer
    
en, seems declare variables and then assign separately. Anyway, this is not big problem, I just take some habits from one function elsewhere. –  HyperGroups May 24 '13 at 4:07
    
@Hyper, yes, that's precisely what m_goldberg does. That's how it is... –  J. M. May 24 '13 at 8:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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