MathGroup Archive 2006

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

Search the Archive

Re: simple question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg68670] Re: [mg68643] simple question
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Wed, 16 Aug 2006 03:35:56 -0400 (EDT)
  • References: <200608141044.GAA25337@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 14 Aug 2006, at 12:44, corrallia81 at yahoo.com wrote:

> Hello,
>
> I know it is simple but I can't find something.
>
> In[1]:= lst=Range[10]
> Out[1]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
>
> In[2]:= Reverse[lst]
> Out[2]={10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
>
> How we can get the same result with pattern matching?
>
> My unsuccesful attempts include
>
> In[9]:=Range[10]/.{x_,y___}->{y,x}
>
> Out[9]={2, 3, 4, 5, 6, 7, 8, 9, 10, 1}
>
> In[10]:=
> Range[10]//.{x_,y___}->{y,x}
>
>> From In[10]:=
> ReplaceRepeated::"rrlim": "Exiting after \!\({1, 2, 3, 4, 5, 6, 7, 8,
> 9, \
> 10}\) scanned \!\(65536\) times."
>
> Out[10]=
> {7, 8, 9, 10, 1, 2, 3, 4, 5, 6}
>
> Can I add something to previous rules to work?
>
> Thanks for any help.
>


One way is:


lst=Range[10];


lst//.{a___,x_,y_,b___}/;OrderedQ[{x,y}]:>{a,y,x,b}

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

but you should be aware that this is a very inefficient way to  
achieve this result.


Andrzej Kozlowski


  • Prev by Date: Re: Re: using answer form reduce
  • Next by Date: Re: Re: MemberQ
  • Previous by thread: simple question
  • Next by thread: Re: simple question