I often find myself writing code that looks a bit like this:

f[x_Integer] := 
     range = Range[2] + x
       a = range[[1]],
       b = range[[2]],
       c = g[range]

It would be nice if I could avoid Withs and just write

f[x_Integer] := 
   range = Range[2] + x,
   {a,b} = range,
   c = g[range]

which would then automatically expand to the above at definition time.

What I'm asking is a bit similar to this question. There are additional requirements however. The new scoping construct (Let in the above) should:

  • Group sequential disjoint assignments into single Withs.
  • Thread over List assignments.

Of course, it should not evaluate the left-hand-sides and the right-hand-sides of the assignments while expanding to Withs.

Any proposals for such a scoping construct? (I'll post my version soon).

  • $\begingroup$ Your example doesn't need any scoping: f[x_Integer] := h[Sequence@@#,g@#]&@(Range[2]+x) $\endgroup$ – Bob Hanlon Nov 5 '14 at 14:24
  • $\begingroup$ Why do you want to rewrite using With rather than preserving a higher abstraction such as LetL? $\endgroup$ – Mr.Wizard Nov 5 '14 at 14:56
  • $\begingroup$ Also: what is the reason to prefer With over Module? Assignments such as {a,b} = range are simpler with the latter. $\endgroup$ – Mr.Wizard Nov 5 '14 at 15:00
  • $\begingroup$ @Mr.Wizard This should be a generalization of LetL, which doesn't do the two points I mentioned (Leonid's answer below does). Module doesn't allow you to do threaded assignments in the first argument, forcing you to write Module[{a,b},{a,b}=Range[2];...], which is duplication I don't like. Also, it'd like to inject into held expressions -- another reason not to go with Module. $\endgroup$ – Teake Nutma Nov 5 '14 at 15:07
  • 2
    $\begingroup$ If you are ok with undocumented features, you can supply multiple arguments to With in Mathematica 10.3 and above: e.g. With[{c = d}, {b = c}, {a = b}, a] (You'll have to also tolerate the red syntax coloring in the front end, or turn it off manually) [and I just realized that it doesn't satisfy your second requirement of threading over List assignments] $\endgroup$ – QuantumDot May 4 '16 at 20:03

With this helper function:

SetAttributes[partThread, HoldAll];
partThread[l___, rhs_] :=
  Join @@ Replace[
    MapIndexed[Append[#, First@#2] &, Thread[Hold[{l}]]],
    Hold[s_, i_] :> Hold[s = rhs[[i]]],

The following modification of LetL seems to work according to your specs:

ClearAll[Let, let];
SetAttributes[{Let, let}, HoldAll];

Let /: Verbatim[SetDelayed][lhs_, rhs : HoldPattern[Let[__, _]]] := 
   Block[{With}, Attributes[With] = {HoldAll};
      lhs := Evaluate[rhs /. HoldPattern[With[{}, b_]] :> b]

Let[args___, body_] := let[{args}, body, {}, {}];

let[{}, body_, {}, _] := With[{}, body];

let[{Set[{s___}, rhs_], rest___}, body_, dec_, syms_] :=
     partThread[s, temp] /. Hold[d___] :>
        let[{temp = rhs, d, rest}, body, dec, syms]

   {Set[sym_, rhs_], rest___}, 
] /; FreeQ[Unevaluated[rhs], Alternatives[syms]] :=
     let[{rest}, body, {decs, sym = rhs}, {syms, HoldPattern[sym]}];

let[{args___}, body_, {decs__}, _] :=
     Attributes[With] = {HoldAll};
     With[{decs},Evaluate[let[{args}, body, {}, {}]]]

This works quite similarly to LetL. What it does in addition to LetL is that it collects previous declarations into auxiliary lists stored as extra arguments of let, so that it can group together disjoint declarations. It also threads over arguments, using the partThread helper function. In all other respects it is the same code as LetL.

Here is your example:

f[x_Integer] := 
   Let[range = Range[2] + x, {a, b} = range, c = g[range], h[a, b, c]];

we can check what was generated:


  • $\begingroup$ @MichaelE2 That was an evaluation leak, thanks for reporting. I seem to have fixed it, although perhaps not very elegantly. $\endgroup$ – Leonid Shifrin Nov 5 '14 at 14:40
  • $\begingroup$ You're welcome. It seems fixed now. $\endgroup$ – Michael E2 Nov 5 '14 at 14:51
  • $\begingroup$ @MichaelE2 Thanks for double-checking. I've not done this sort of things for some while, got a bit rusty. $\endgroup$ – Leonid Shifrin Nov 5 '14 at 14:52
  • $\begingroup$ That's a bit more elegant than what I had :). One question though: why doesn't Alternatives leak evaluation here? $\endgroup$ – Teake Nutma Nov 5 '14 at 15:09
  • $\begingroup$ @TeakeNutma because I wrapped symbols in HoldPattern, and use Unevaluated for the rhs. HoldPattern is exactly the right tool for the job here, since it is invisible to the pattern-matcher (and so FreeQ works fine). $\endgroup$ – Leonid Shifrin Nov 5 '14 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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