0
$\begingroup$

I know we have

a := 1
b := 1

but is there some 1-liner shorthand, akin to:

a, b := 1 (* I know this doesn't work, but I'm just using it as an example *)

?

$\endgroup$
  • 2
    $\begingroup$ It works for = which is what is enough for 1 anyway. Or do you insist on SetDelayed? $\endgroup$ – Kuba Jun 10 at 20:23
  • 4
    $\begingroup$ You can try a=b=1 $\endgroup$ – bill s Jun 10 at 20:37
  • $\begingroup$ Is there a way to make this work with lazy evaluation (i.e., :=)? $\endgroup$ – George Jun 10 at 20:38
  • 2
    $\begingroup$ @George The := assignment is delayed, not lazy. For lazy you need memoization as well. $\endgroup$ – Roman Jun 10 at 21:32
  • $\begingroup$ @Kuba and @"bill s" seem to have the answer and should post? $\endgroup$ – Mark R Jun 11 at 7:42
1
$\begingroup$

You can define a function that does what you want:

SetAttributes[DotEqual, HoldAll]
DotEqual[a__, v_] := With[
    {rhs = Thread[Table[Hold[v],Length[Hold[a]]], Hold]},
    Replace[rhs, Hold[x_] :> ({a} := x)];
]

Then:

Clear[a, b];
x := Print[1];
DotEqual[a, b, x]

This produces the desired OwnValues with no evaluation leaks:

OwnValues[a]
OwnValues[b]

{HoldPattern[a] :> x}

{HoldPattern[b] :> x}

The reason I chose DotEqual as the function name is that it already has an infix notation (entered by using Esc . =). So:

Clear[a, b];
x := Print[1];
a ≐ b ≐ x

Check:

OwnValues[a]
OwnValues[b]

{HoldPattern[a] :> x}

{HoldPattern[b] :> x}

$\endgroup$

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.