Re: Assigning part of indexed object
- To: mathgroup at smc.vnet.net
- Subject: [mg119845] Re: Assigning part of indexed object
- From: Oleksandr Rasputinov <oleksandr_rasputinov at hmamail.com>
- Date: Sat, 25 Jun 2011 05:28:21 -0400 (EDT)
- References: <itsjn7$8u7$1@smc.vnet.net> <itv7ve$pbu$1@smc.vnet.net> <iu1tp3$9kr$1@smc.vnet.net>
Regarding consistency, it is worth noting that there is a
problem with ReplacePart as a direct substitute for Set:
m = Range[5];
ReplacePart[m, 1 -> 10] :> {10, 2, 3, 4, 5}
but
m[[1]] = 10 :> ( m = {10, 2, 3, 4, 5}; 10 ) :> 10
the following is thus a better solution than what I had
posted previously:
Unprotect[Set];
Set[sym_[[part_]], val_] := (
sym = ReplacePart[sym, part -> val];
sym[[part]]
);
Protect[Set];
Now:
Clear[m]; (* OwnValues will override DownValues *)
m[1] = Range[5];
m[1][[1]] = 10 :> ( m[1] = {10, 2, 3, 4, 5}; 10 ) :> 10
which is consistent with other uses of Set.
On Jun 24, 12:52 pm, "Fabrice P. Laussy" <fabrice.lau...@n0spam-
gmail.com> wrote:
> Dear all,
>
> Thanks for the solution. Supplemented with Oleksandr Rasputinov's fix:
>
> > Unprotect[Set];
> > Set[sym_[[part_]], val_] := sym = ReplacePart[sym, part -> val];
> > Protect[Set];
>
> it works splendidly.
>
> In "some elements of Mathematica Design (1992)" [http://goo.gl/nm1tT],
> Stephan Wolfram makes a point where one should resist the temptation to
> design functions to do things that look natural for them to do, as they
> could break dramatically later on.
>
> Here we have a clear case where Set seems it ought to work as redesigned by
> Oleksandr. Is there any reason for this not being the default behaviour?