Re: functional code
- To: mathgroup at smc.vnet.net
- Subject: [mg4807] Re: functional code
- From: Paul Abbott <paul at physics.uwa.edu.au>
- Date: Mon, 16 Sep 1996 23:51:28 -0400
- Organization: University of Western Australia
- Sender: owner-wri-mathgroup at wolfram.com
richard j. gaylord wrote: > Given a list of numbers row={18,19,1,11,25,12,22,14} > Select the numbers from the list by taking the largest number > from the ends of the list until the list is empty. Here are two straightforward pattern-matching solutions: In[1]:= row={18,19,1,11,25,12,22,14}; In[2]:= {{},row}//.{{d___},{a_,b___,c_}}:> {{d,Max[a,c]},If[a<c,{a,b},{b,c}]}//Flatten Out[2]= {18, 19, 14, 22, 12, 25, 11, 1} In[3]:= {{},row}//.{ {{d___},{a_,b___,c_}}/;a<c :> {{d,c},{a,b}}, {{d___},{a_,b___,c_}}/;a>=c :> {{d,a},{b,c}}}//Flatten Out[3]= {18, 19, 14, 22, 12, 25, 11, 1} Cheers, Paul _________________________________________________________________ Paul Abbott Department of Physics Phone: +61-9-380-2734 The University of Western Australia Fax: +61-9-380-1014 Nedlands WA 6907 paul at physics.uwa.edu.au AUSTRALIA http://www.pd.uwa.edu.au/Paul Black holes are where God divided by zero _________________________________________________________________ ==== [MESSAGE SEPARATOR] ====