MathGroup Archive 2010

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

Search the Archive

Re: Sort within self-defined procedure

  • To: mathgroup at smc.vnet.net
  • Subject: [mg108606] Re: Sort within self-defined procedure
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Thu, 25 Mar 2010 04:24:22 -0500 (EST)

Your own code explains it perfectly.

ks[x_] = Module[{l}, l = {1, 5, x, 2};
   Sort[l, Greater]]

{5, x, 2, 1}

The result is not a Module... it's precisely the list above:

?ks

Global`ks

ks[x_]={5,x,2,1}

Hence, when you try ks[20] later, it naturally puts 20 in the place of x.

What you needed was SetDelayed (":=") not Set ("=").

For instance,

ks[x_] :=
  Sort[{1, 5, x, 2}, Greater]
ks[21]

{21, 5, 2, 1}

(Module wasn't needed, either way.)

Bobby

On Wed, 24 Mar 2010 04:42:34 -0500, dornakul <postitdummy at arcor.de> 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
>


-- 
DrMajorBob at yahoo.com


  • Prev by Date: Re: Sort within self-defined procedure
  • Next by Date: Re: estimation
  • Previous by thread: Re: Sort within self-defined procedure
  • Next by thread: Re: Sort within self-defined procedure