Re: Pattern matching
- To: mathgroup at smc.vnet.net
- Subject: [mg4894] Re: Pattern matching
- From: Clemens Frey <Clemens.Frey at uni-bayreuth.de>
- Date: Fri, 4 Oct 1996 00:17:34 -0400
- Organization: uni-bayreuth.de
- Sender: owner-wri-mathgroup at wolfram.com
Reinhard Simonovits wrote: > > Dear Mathgroupers, > In the list of two dim vectors vecs = { { {-1,1}, {2,2} }, {3,4} } in a rectangular coord systems, the last > vector is a radius vector. > To plot them I have to add {0,0}, so that plotvecs = { { {-1,1}, {2,2} }, { {0,0}, {3,4} } } fulfills the task. > > This can be done with > plotvecs = Apply[ If[ NumberQ[#1], { {0,0}, {##} }, {#1,#2} ]&, vecs, {1} ] > > I wonder how could this be done by pattern matching. > > plotvecs = vecs /. {a_,b_} -> { {0,0},{a,b} } /; ??? > > Thanks in advance, Reinhard Dear Reinhard, just try the following vecs = {{{-1,1},{2,2}}, {3,4}}; plotvecs = vecs /. {l1_List,l2_List} -> {l1,{{0,0},l2}}; Then the last vector of your list (the radius) will be transformed as you intended. Use l1__List (BlankSequence) or l1___List (BlankNullSequence) if there could be one or more vectors at the beginning of the list or none or more, respectively. Be careful with the conditional pattern you tried. First of all, a thing like -> ... /; is obsolete. Use :> ... /; instead. Second, it's dangerous as the {-1,1} in the first vector would always match your condition for the radius vector {3,4}, just because these pairs are of the same shape... Last, consider the use of the function ReplacePart. I hope these hints help Greetings Clemens Frey (-: Clemens.Frey at uni-bayreuth.de :-) ==== [MESSAGE SEPARATOR] ====