MathGroup Archive 1995

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

Search the Archive

Re: switch list elements, index from inside ??

  • To: mathgroup at smc.vnet.net
  • Subject: [mg2672] Re: [mg2644] switch list elements, index from inside ??
  • From: Allan Hayes <hay%haystack at smc.vnet.net>
  • Date: Mon, 4 Dec 1995 01:41:22 -0500

davep at newton.umsl.edu
in [mg2644] switch list elements, index from inside ??
writes

>I have a problem regarding the interchanging of list elements  
(using >the routine posted by Richard Mercer)
>
>   t={1,2,3,4,5,6,7,8,9,10,11};
>   s[list_List]:=
>      ReplacePart[ReplacePart[list,list[[n]],m],list[[m]],n]//.     

>         {m->Random[Integer,{1,10}],n->Random[Integer,{1,10}]}
>   s[t]
>
>Mma complains that the part and position specification (that's m  
and >n) in ReplacePart is neither an integer nor a list of non zero  
>integers.Why doesn't Mma accept the (Random-) Integers ?

The part ReplacePart[ReplacePart[list,list[[n]],m],list[[m]],n] is  
evaluated before m and n are replaced by the integers generated.
You can use Unevaluated to prevent this:

   t={1,2,3,4,5,6,7,8,9,10,11};
   s[list_List]:=
      Unevaluated[
         ReplacePart[ReplacePart[list,list[[n]],m],list[[m]],n]
      ]//.{m->Random[Integer,{1,10}],n->Random[Integer,{1,10}]}

   s[t]


   {6, 2, 3, 4, 5, 1, 7, 8, 9, 10, 11}

Allan Hayes
hay at haystack.demon.co.uk





  • Prev by Date: Re: [Q] how fast is each machine with Mathematica ?
  • Next by Date: RE: MathLink
  • Previous by thread: Re: switch list elements, index from inside ??
  • Next by thread: Re: switch list elements, index from inside ??