Skip to content

Module ​

Evaluates an expression with local variables, fresh on every call.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageModule✓
Module({x, y = y0, ...}, body)bodybody, evaluated with xx, yy, ... as fresh local variables (each initialized, or left unbound, per its own binding) invisible outside the call.
Typed signatures
TypePackageApplies
(any, any) -> anycombinatoricsalways

Domain: Collections

Details
  • Each call gets its own scope, so a recursive or repeated ModuleModule call never has its locals collide with an outer binding — or another call's — of the same name.
  • An initializer's value (the y0y0 in y=y0y = y0) is computed in the OUTER context, before any local shadows it, same as With — a later binding cannot read an earlier one's local value.
  • A binding with no = value (a bare xx) declares an uninitialized local; referencing it before assigning gives an unevaluated symbol, not an error.

Examples

Basic #

a local counter, invisible outside

live ↗
InModule([k=0],k:−k+1)\mathrm{Module}(\bigl\lbrack k=0\bigr\rbrack, k\coloneq k+1)Out11
Scope #

two calls' locals of the same name don't collide

live ↗
In[Module([n=10],n+1),Module([n=20],n+1)]\bigl\lbrack\mathrm{Module}(\bigl\lbrack n=10\bigr\rbrack, n+1), \mathrm{Module}(\bigl\lbrack n=20\bigr\rbrack, n+1)\bigr\rbrackOut[11,21]\bigl\lbrack11, 21\bigr\rbrack
About
Examples2
Checked againstWolfram Language 1 agree
CrosswalkWolfram Language

See also: With