Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: basic issue with do and :=

  • To: mathgroup at smc.vnet.net
  • Subject: [mg60409] Re: basic issue with do and :=
  • From: albert <awnl at arcor.de>
  • Date: Fri, 16 Sep 2005 03:48:50 -0400 (EDT)
  • Organization: Arcor
  • References: <dgbevg$fol$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

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}]
> ---------------------------

this is probably closest to what you expect:

Do[With[{ii=i},b[ii]:=a[ii]+1],{i,1,10}];

if you want to understand how this works, you would need to read the
chapters about how evaluation works within the helpbrowser. 

Anyway, probably you will be happier to use (no do-loop requiered!):

a[i_] = i
b[i_] := a[i]+1

this will work for all i (not only i=1,2,3,4,5,6,7,8,9,10). If you want only
integers or only values between 0 and 10 you could also do something like:

a[i_Integer] = i
b[i_Integer] := a[i]+1

or 

a[i_Integer/;0<=i<=10] = i
b[i_Integer/;0<=i<=10] := a[i]+1

if all this doesn't look obvious to you, you should read the chapters about
pattern matching in the online help...

good luck,

Albert


  • Prev by Date: Re: basic issue with do and :=
  • Next by Date: Re: Proposal to get Stephen to Improve the lot ofSpace Shuttle Programmers
  • Previous by thread: Re: basic issue with do and :=
  • Next by thread: Re: basic issue with do and :=