       Re: composing functions

• To: mathgroup at smc.vnet.net
• Subject: [mg45544] Re: [mg45527] composing functions
• From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
• Date: Tue, 13 Jan 2004 04:03:58 -0500 (EST)
• References: <200401120715.CAA29367@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```On 12 Jan 2004, at 07:15, Pedro L wrote:

> 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:= kk = {{f, g, h}, {a, b, c, d}}
>
> Out= {{f, g, h}, {a, b, c, d}}
>
> In:= result1 = {kk[[2,1]]}; For[i = 1, i < Length[kk[]], i++,
> AppendTo[result1, kk[[1,i]]];
>             AppendTo[result1, kk[[2,i + 1]]]]; result1
>
> Out= {a, f, b, g, c, h, d}
>
> In:= result2 = StringJoin @@ ToString /@ result1
>
> Out= afbgchd
>
> In:= result3 = StringInsert[result2, "~", Range[2,
> StringLength[result2]]]
>
> Out= a~f~b~g~c~h~d
>
> In:= result4 = ToExpression[result3]
>
> Out= h[g[f[a, b], c], d]
>
>
>
>
> But I'm really sure that it can be done much better.
>
> Could you help me?
>
>
>
>
>
One possible way:

ListCompose[l_List, k_List] := Module[{F = l},
Fold[First[F = RotateRight[F]][#1, #2] & , First[k],
Rest[k]]]

Now:

ListCompose[{h, g, f}, {a, b, c, d}]

Out=
h[g[f[a, b], c], d]

ListCompose[{h, g, f, k}, {a, b, c, d, e}]

h[g[f[k[a, b], c], d], e]

etc.

Andrzej Kozlowski
Chiba, Japan
http://www.mimuw.edu.pl/~akoz/

```

• Prev by Date: what is the general theory of extracting solutions from DSolve (and similar) functions
• Next by Date: Re: composing functions
• Previous by thread: composing functions
• Next by thread: Re: composing functions