 
 
 
 
 
 
Re: Recursion
- To: mathgroup at smc.vnet.net
- Subject: [mg25946] Re: [mg25925] Recursion
- From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
- Date: Fri, 10 Nov 2000 02:40:22 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Here is a complicated but single line recursive function that almost
satisfies your requirements:
f2 = Function[l, If[Length[l] == 1, {{}, l}, Function[ Which[#2 == {}, #1,
#1 == {}, #2, True, Prepend[#0[Rest[#1], #2], First[#1]]]][f2[Rest[l]],
Function[ If[#2 == {}, {}, Prepend[#0[#1, Rest[#2]], #1[First[#2]]]]][
Function[Append[#, First[l]]], f2[Rest[l]]]]]]
For example:
In[14]:=
f2[{a, b, c}]
Out[14]=
{{}, {c}, {b}, {c, b}, {a}, {c, a}, {b, a}, {c, b, a}}
What actually happens is that the functions Join and Map are defined
recursively as anonymous functions inside the code. So this is just my
pervious solution re-written to suit your requirements.
Unfortunately there is one snag. The function makes use of Mathematica's
unique (I think!) construction involving #0. It seems to me that you are
writing programs in Scheme or a similar Lisp based language and none of them
have anything similar.
There may well be a much simpler one line functional recursive solution
(without using auxiliary functions -- the problem becomes easy if you use
them) but I can't think of one.
-- 
Andrzej Kozlowski
Toyama International University
JAPAN
http://platon.c.u-tokyo.ac.jp/andrzej/
http://sigma.tuins.ac.jp/
on 00.11.9 9:19 PM, Carlos Baeta Silva at carlos.silva at tcontas.pt wrote:
> Andrzej Kozlowski, thanks for your help, but I forgot to tell that we only
> can use the functions: First, Last, Rest, {}, ==, !=,[[]],Append,
> Prepend,Take e Drop.
> 
> In the first question I define f in this terms:
> 
> f = Function[p,
> If[p=={},
> {{},{}},
> If[Last[First[p]]<9.5,
> Prepend[f[Rest[p]],First[p]],
> Append[f[Rest[p]],First[p]]
> ]
> ]
> ]
> 
> In the second question I have no idea, Can you help me Andrzej?
> 
> Thanks a lot!
> 
> -----Mensagem original-----
> De: Andrzej Kozlowski [mailto:andrzej at tuins.ac.jp]
> Enviada: quinta-feira, 9 de Novembro de 2000 12:01
> Para: Carlos Baeta Silva; mathgroup at smc.vnet.net
> Assunto: Re: [mg25925] Recursion
> 
> 
> 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.

