Re: Working in Modules
- To: mathgroup at smc.vnet.net
- Subject: [mg21375] [mg21086] Re: Working in Modules
- From: "Stephen P Luttrell" <luttrell at signal.dra.hmg.gb>
- Date: Fri, 31 Dec 1999 21:30:10 -0500 (EST)
- Organization: Defence Evaluation and Research Agency
- References: <7a2c0d$1ul@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
>This is my third attempt to post this question. No idea why it doesnt
>work. I'm not an expert in Mathematica and would appreciate comments on
>why init1 returns a list with all zeros replaced by -1, but init does
>not. Its probably very simple but its confused me.
>
>Don
>
>init[n_Integer]:=Module[
> {s=Table[Random[Integer],{n n}]},
> s/.{0->-1};
> Partition[s,n]
> ]
>
>init1[n_Integer]:=Module[
> {s=Table[Random[Integer],{n n}]},
> s/.{0->-1}
> ]
You need to change init to read as follows:
init[n_Integer]:=Module[
{s=Table[Random[Integer],{n n}],s1},
s1=s/.{0->-1};
Partition[s1,n]
]
In your version where you do
s/.{0->-1};
Partition[s,n]
the result of s/.{0->-1}; is not assigned back to s as you have assumed.
Stephen P Luttrell
Signal Processing and Imagery Department
DERA Malvern, St.Andrew's Road
Malvern, United Kingdom, WR14 3PS
+44 (0)1684 894046 (tel)
+44 (0)1684 894384 (fax)
luttrell at signal.dera.gov.uk (email)