       Re: composing functions

• To: mathgroup at smc.vnet.net
• Subject: [mg45536] Re: composing functions
• From: bobhanlon at aol.com (Bob Hanlon)
• Date: Tue, 13 Jan 2004 04:03:52 -0500 (EST)
• References: <btthta\$so5\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```This is slightly more compact than my first response

kk={{f,g,h},{a,b,c,d}};

n=1;Fold[kk[[1,n++]][#1,#2]&, kk[[2,1]],Rest[kk[]]]

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

Bob Hanlon
_____________________________

kk={{f,g,h},{a,b,c,d}};

n=0;Fold[(n++;kk[[1,n]][#1,#2])&, kk[[2,1]],Rest[kk[]]]

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

Bob Hanlon

In article <btthta\$so5\$1 at smc.vnet.net>, Pedro L <pedrito6 at softhome.net> wrote:

<< 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]

```

• Prev by Date: RE: composing functions
• Next by Date: Re: composing functions
• Previous by thread: RE: composing functions
• Next by thread: Re: composing functions