MathGroup Archive 2004

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

Search the Archive

Re: composing functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg45556] Re: composing functions
  • From: poujadej at yahoo.fr (Jean-Claude Poujade)
  • Date: Wed, 14 Jan 2004 01:26:11 -0500 (EST)
  • References: <btthta$so5$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Pedro L <pedrito6 at softhome.net> wrote in message news:<btthta$so5$1 at smc.vnet.net>...
> Hi!
> 
> I  would  like  to find a way for composing a list of functions over a
> list of numbers.
> 
> I  have  the  list  {{f,  g,  h},  {a,  b, c, d}}
> (f, g, h are functions and a, b, c, d are numbers)
> I would like to obtain h[g[f[a, b], c], d]
> 
> Since  it's  for a real program, I had to do it in "any" way. So I did
> it as I show below:
> 
> 
> In[1]:= kk = {{f, g, h}, {a, b, c, d}}
> 
> Out[1]= {{f, g, h}, {a, b, c, d}}
> 
> In[2]:= result1 = {kk[[2,1]]}; For[i = 1, i < Length[kk[[2]]], i++, AppendTo[result1, kk[[1,i]]]; 
>             AppendTo[result1, kk[[2,i + 1]]]]; result1
> 
> Out[2]= {a, f, b, g, c, h, d}
> 
> In[3]:= result2 = StringJoin @@ ToString /@ result1
> 
> Out[3]= afbgchd
> 
> In[4]:= result3 = StringInsert[result2, "~", Range[2, StringLength[result2]]]
> 
> Out[4]= a~f~b~g~c~h~d
> 
> In[5]:= result4 = ToExpression[result3]
> 
> Out[5]= h[g[f[a, b], c], d]
> 
> 
> 
> 
> But I'm really sure that it can be done much better.
> 
> Could you help me?

This is my solution:

In[1]:=transform[fgh_List, abcd_List]:=
   First[{fgh, abcd} //.
   {{{f_,ff___},{a_,b_,bb___}} -> {f[a,b],{ff},{bb}},
   {ff_,{g_,h___},{c_,d___}} -> {g[ff,c],{h},{d}}}];

In[2]:=transform[{f,g,h,i},{a,b,c,d,e}]
Out[2]=i[h[g[f[a,b],c],d],e]

hth
---
jcp


  • Prev by Date: Re: what is the general theory of extracting solutions from DSolve (and similar) functions
  • Next by Date: Re: how to 'edit' MatrixForm output to create a new matrix?
  • Previous by thread: RE: composing functions
  • Next by thread: Symplectic integration and Interpolating function