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

1 Answer 1

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 and acknowledge you have read our privacy policy.

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