11
$\begingroup$

In the chapter 7 of the Power Programming with Mathematica, there is a paragraph discussing the behavior of Set- functions: (page 205. (pdf version p. 224))

However, I can't get the author's meaning. It may due to the material itself, or due to the reason that I'm not native speaker in English. I totally don't get the red line part, as well unable to see how the word "because" is used here, what is the logical inference here? enter image description here

Also, I can't get what author mean in the blue part in next page. Need help.enter image description here

Update question: If it would not evaluate Parts of the left hand side, then except for the reason that assigning to variables in a List was no longer possible (e.g. {a,b,c}={1,2,3}), is there other decided reason or critical situation that something would not work well, so we would really appreciate if such partial evaluation is enabled?

$\endgroup$
  • $\begingroup$ About update, if I understand your question well it seems that memoization/caching could be tricky. e.g. temp=1; f[temp]=2; temp=3;f[temp]=4]. $\endgroup$ – Kuba Mar 22 '18 at 7:29
11
$\begingroup$

He is simply saying that the first argument of Set is not evaluated before Set creates the definition.

However, the sub-parts of the first argument are all evaluated, including the head(i.e. part 0).

Thus in

f[x] = y

f[x] is not evaluated in the sense that any previous definition similar to f[z_] := z^2 will be ignored. However, f is evaluated and x is evaluated. Furthermore, any Hold-attributes f might have will prevent evaluation as usual.


Through examples:

Given these definitions,

ClearAll[f, x, foo]

f[a_] := foo

x = 1;

what new definitions will the following command create?

f[x] = y
(* y *)

It will not assign to foo because f[x] won't evaluate. It will also not assign to f[x] because x will evaluate. It will instead assign to f[1].

?f

Global`f

f[1]=y

f[a_]:=foo

However, if f has the HoldAll (or similar) attributes, Set will honour this before evaluating parts of its left-hand-side.

ClearAll[f, x, foo]
SetAttributes[f, HoldAll];

f[a_] := foo

x = 1;

f[x] = y
(* y *)

?f

Global`f

Attributes[f]={HoldAll}

f[x]=y

f[a_]:=foo
$\endgroup$
  • $\begingroup$ (Assuming the following discussion is without Hold- attributes.) So if the lhs of =(Set) is atomic symbol, then it is held. If the lhs of Set is in contrast normal equation, namely of the form h[expr1,expr2,...], then h is evaluated, expr1, expr2, ... are also evaluated? But being that said, if there is a direct global rule defined beforehand that can match h[expr1,expr2,...], then the kernel would pretend not knowing it. $\endgroup$ – Eric Mar 19 '18 at 14:39
  • $\begingroup$ Is my understanding correct? $\endgroup$ – Eric Mar 22 '18 at 7:11
7
$\begingroup$

If Set would not have the HoldFirst attribute, it would lead to undesired behavior if variables on the left hand side have already values assigned to them. Here is an emulation of that:

set[a_, b_] := Set[a, b]
ClearAll[a, b, c]
set[a, 1]
set[a, 2]
a

1

Set::setraw: Cannot assign to raw object 1.

2

1

If it would not evaluate Parts of the left hand side, then assigning to variables in a List was not possible (this can be considered as syntax sugar). Here I emulate what would happen if Set would handle List on its left hand side like any other head:

ClearAll[a, b, c, list];
set[list[a, b, c], list[1, 2, 3]];
{a, b, c}
list[a, b, c]

{a, b, c}

list[1, 2, 3]

$\endgroup$
  • $\begingroup$ Is there a typo of the result on the very last line list[a, b, c]? My 10.4 version gave me the answer list[1,2,3]. $\endgroup$ – Eric Mar 19 '18 at 14:56
  • $\begingroup$ @Eric Jepp. Fixed. $\endgroup$ – Henrik Schumacher Mar 19 '18 at 15:01
  • $\begingroup$ If it would not evaluate Parts of the left hand side, then except for the reason that assigning to variables in a List was no longer possible (e.g. {a,b,c}={1,2,3}), is there other decided reason or critical situation that something would not work well, so we would really appreciate if such partial evaluation is enabled? $\endgroup$ – Eric Mar 22 '18 at 4:20
  • 1
    $\begingroup$ I would sey no, there is no other reason, at least no one that comes to my mind. Of course, I don't know for sure. $\endgroup$ – Henrik Schumacher Mar 22 '18 at 7:03

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.