MathGroup Archive 1995

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

Search the Archive

Re: Programming: List Structure Manipulation

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg901] Re: Programming: List Structure Manipulation
  • From: rubin at msu.edu (Paul A. Rubin)
  • Date: Sun, 30 Apr 1995 03:35:30 -0400
  • Organization: Michigan State University

In article <3nck7m$a07 at news0.cybernetics.net>,
   Xah Y Lee  <xyl10060 at fhda.edu> wrote:
->Can anyone show me better ways to write the following function?
->
->fun[listA_, styleList_]:=
->   Table[
->      Append[
->          Flatten@{ First@RotateLeft[styleList,t-1] },  listA[[t]]
->      ],
->      {t, Length@listA}
->  ]
->
[snip]
The following, somewhat cumbersome, definitions produce a function "fun" 
(defined, for convenience, in terms of another function "fun1") which 
matches all your examples and seems tolerably quick.  There is probably a 
more efficient way of coding it, but at the moment this is the best that 
comes to mind.

Clear[ fun, fun1 ];
fun1[ x_List, y_List ] := MapThread[ fun1, {x, y} ] /; 
  Length[ x ] == Length[ y ]
fun1[ x_List, y_List ] := 
  Module[
    {z = Partition[ x, Length[ y ] ], w},
    w = Drop[ x, Length[ Flatten[ z, 1 ] ] ];
    If[ w != {}, z = Append[ z, w ] ];
    fun1[ #, y ]& /@ z
  ]/; Length[ x ] > Length[ y ]
fun1[ x_List, y_List ] := 
  fun1[ x, Take[ y, Length[ x ] ] ] /; Length[ x ] < Length[ y ]
fun1[ x_, y_ ] := Flatten[ {y, x} ] /; Head[ x ] =!= List
fun[ x_, y_ ] := Flatten[ fun1[ x, y ], 1 ]


Paul

**************************************************************************
* Paul A. Rubin                                  Phone: (517) 432-3509   *
* Department of Management                       Fax:   (517) 432-1111   *
* Eli Broad Graduate School of Management        Net:   RUBIN at MSU.EDU    *
* Michigan State University                                              *
* East Lansing, MI  48824-1122  (USA)                                    *
**************************************************************************
Mathematicians are like Frenchmen:  whenever you say something to them,
they translate it into their own language, and at once it is something
entirely different.                                    J. W. v. GOETHE


  • Prev by Date: Re: PlotRange Limitation?
  • Next by Date: Re: help w/ SphericalPlot3D
  • Previous by thread: Programming: List Structure Manipulation
  • Next by thread: Random[Real,{0,1},$MachinePrecision] isn't MachineNumber[] ???!!