Re: basic issue with do and :=
- To: mathgroup at smc.vnet.net
- Subject: [mg60426] Re: basic issue with do and :=
- From: dh <dh at metrohm.ch>
- Date: Fri, 16 Sep 2005 03:49:06 -0400 (EDT)
- References: <dgbevg$fol$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi David,
this may be confusing, but it is correct. Consider:
Do[b[i] :=a[i]+1, {i, 10}]
As you can read in the manual, Do temporarily sets i to 1,2...,
effectively using Block. Outside Do, i has the original value.
However, := does not evaluate its right hand side, but stores it in
unevaluated form. When you later use b[i], i is evaluated to whatever
value it has at this time.
sincerely, Daniel
David wrote:
> Hi all,
>
> this works as (I) expected
>
> -------------------------
> Do[a[i] = i, {i, 10}]
> Do[b[i] =a[i]+1, {i, 10}]
> -------------------------
>
> While this doesn't:
>
> --------------------------
> Do[a[i] = i, {i, 10}]
> Do[b[i] :=a[i]+1, {i, 10}]
> ---------------------------
>
> [note ":=" instead of "=" on the second line].
>
> namely the output is: "b[2]:=a[i]+1" for all i
>
>
>
> Is there any compact way to define: "b[i] :=a[i]+1" for all i ?
>
>
> Thank you in advance,
>
> David
>