Re: Sort within self-defined procedure
- To: mathgroup at smc.vnet.net
- Subject: [mg108612] Re: Sort within self-defined procedure
- From: Murray Eisenberg <murray at math.umass.edu>
- Date: Thu, 25 Mar 2010 04:25:32 -0500 (EST)
Change ks[x_] = to ks[x_]:= .
The difference is whether you evaluate the right-hand side of the
function definition immediately when you define it (which is what your
use of = does) or whether, instead, you evaluate the right-hand side
only when you call ks.
So the behavior you observed is completely consistent with:
Clear[x]
Sort[{1, 5, x, 2}, Greater]
{5, x, 2, 1}
On 3/24/2010 5:42 AM, 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
>
--
Murray Eisenberg murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305