Re: Sort within self-defined procedure
- To: mathgroup at smc.vnet.net
- Subject: [mg108613] Re: Sort within self-defined procedure
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Thu, 25 Mar 2010 04:25:44 -0500 (EST)
- References: <hocm9o$shi$1@smc.vnet.net>
dornakul wrote:
> Hi,
>
> I am desperate. Why is it not possible to sort a list within a self-
> defined procedure?
>
> Here comes the minimal example
>
> In[188]:= ks[x_] = Module[{l},
> l = {1, 5, x, 2};
> Sort[l, Greater]
> ]
>
> Out[188]= {5, x, 2, 1}
>
> In[190]:= ks[20]
>
> Out[190]= {5, 20, 2, 1}
>
> Dornakul
> Using Mathematica 7 on a Windows machine
>
Because you defined your procedure using = (Set) rather than :=
(SetDelayed). This meant that the Sort command was executed once when
the procedure was being defined, and not when it was called!
As a general rule, procedures are defined with := and individual values
are assigned using = .
David Bailey
http://www.dbaileyconsultancy.co.uk