Using pure functions to assign a value to variable depending on whether it has been defined already
You say that you want to reproduce the behavior of If[Not[ValueQ[a]], a = 2020]
. This will assign a value and return it if a is not assigned, or return Null if a
already has a value.
I wonder if the following function would work for you then:
ClearAll[condassign]
condassign[variable_Symbol, value_: 2020] := (variable = value)
condassign[variable_?NumericQ, value_: 2020] := Null
The value itself can be given as a second argument, but if omitted it is assumed to be 2020.
Here are two examples:
a =. (* Clear any value in a *)
condassign[a] (* Out: 2020 because a had not value *)
Print@condassign[a] (* Out: Null now that a has a value *)
The Print
expression above is only to show the Null
return value, which otherwise would not appear explicitly.
Clear[a]
If[Head[a] === Symbol, a = 2020]
a (* 2020 *)
a=10;
If[Head[a] === Symbol, a = 2020]
a (* 10 *)