Re: Re: interchanging elements in a list
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg2049] Re: [mg2005] Re: [mg1993] interchanging elements in a list
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Sat, 16 Sep 1995 01:43:38 -0400
Richard Mercer <richard at seuss.math.wright.edu> in [mg2005] Re: [mg1993] interchanging elements in a list gives four ways of interchanging elements (see code below - I add indices to distinguish the four functions). Richard, Interchange1 and Interchange4 are fine but the other two replace by value and can give incorrect results: Interchange2[ {1,1,2},2,3] {2, 2, 1} Interchange3[ {1,1,2},2,3] {2, 2, 1} Allan Hayes hay at haystack.demon.co.uk ****************** Interchange1[list_List,n_,m_]:= ReplacePart[ReplacePart[list,list[[n]],m],list[[m]],n]; Interchange2[list_List,n_,m_]:= list /. {list[[m]]->list[[n]],list[[n]]->list[[m]]}; Interchange3[list_List,n_,m_]:= list /. {#1->#2,#2->#1}& @@ Part[list,{m,n}]; Interchange4[list_List,n_,m_]:= Part[list,Range[1,Length[list]] /. {m->n,n->m}];