MathGroup Archive 2000

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

Search the Archive

Re: Recursion

  • To: mathgroup at smc.vnet.net
  • Subject: [mg25938] Re: [mg25925] Recursion
  • From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
  • Date: Fri, 10 Nov 2000 02:40:16 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Here are a couple of quick attempts (programmed without concern for
efficiency):


In[1]:=
f1[{}] := {{}, {}}; f1[{x_}] := If[Last[x] < 9.5, {{x}, {}}, {{}, {x}}];
f1[{l__, x_}] := 
  If[Last[x] < 9.5, Insert[f1[{l}], x, {1, -1}], Insert[f1[{l}], x, {2,
-1}]]

In[2]:=
f1[{{"poiu", 12}, {"asdfghjkl", 5}, {"qwerty", 18}, {"zxcvbnm", 8}}]

Out[2]=
{{{"asdfghjkl", 5}, {"zxcvbnm", 8}}, {{"poiu", 12}, {"qwerty", 18}}}

In[3]:=
f2[{}] := {{}}; f2[{a_}] := {{a}, {}};
f2[{a_, l__}] := Sort[Join[f2[{l}], Map[Append[#, a] &, f2[{l}]]]]

In[4]:=
f2[{1, 2, 3}]

Out[4]=
{{}, {1}, {2}, {3}, {2, 1}, {3, 1}, {3, 2}, {3, 2, 1}}

In the case of the second function the sorting  is not the same as in your
message but I assumed that that was not important for you. One can fix this
of course but it will make the code even more inefficient.

-- 
Andrzej Kozlowski
Toyama International University
JAPAN

http://platon.c.u-tokyo.ac.jp/andrzej/
http://sigma.tuins.ac.jp/




on 00.11.9 5:04 PM, Carlos Baeta Silva at carlos.silva at tcontas.pt wrote:

> Hello!
> 
> 1. Someone can tell me how I can define a function (using recursion) that
> apllies to a list of this kind:
> 
> {{"poiu", 12},{"asdfghjkl",5},{"qwerty",18},{"zxcvbnm",8}}
> 
> returns {  { {"asdfghjkl",5},{"zxcvbnm",8} }, { {"qwerty",18} },{"poiu",12}
> }  } values <9.5 in one sublist and > in the other.
> 
> And apllies to {} return {{},{}}
> 
> I made this but I can put the sublists inside the empty brackets:
> <<...>> 
> <<...>> 
> 
> 2. Other function (using recursion too) that returns the parts of a list:
> 
> aplies to this {} returns {{}}
> to {{1}} returns {{1},{}} or {{},{1}}
> to {{1,2}} returns {{1,2},{1},{2},{}}
> 
> Thank you very much!
> 
> 




  • Prev by Date: Re: Bug: Mathematica 4.0 vs. Win2k on ThinkPad
  • Next by Date: Re: SDE's
  • Previous by thread: Recursion
  • Next by thread: Re: Recursion