Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: composing functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg45538] Re: composing functions
  • From: Paul Abbott <paul at physics.uwa.edu.au>
  • Date: Tue, 13 Jan 2004 04:03:53 -0500 (EST)
  • Organization: The University of Western Australia
  • References: <btthta$so5$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

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]

Here is one way:

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

  i = 0; Fold[List,First[vars],Rest[vars]] /. List :> Reverse[fs][[++i]]

If you change Fold to have the HoldFirst attribute,

  Unprotect[Fold]
  SetAttributes[Fold, HoldFirst]
  Protect[Fold]

then you can write

 i = 0; Fold[fs[[++i]], First[vars], Rest[vars]]

I'm sure that someone will come up with a more elegant solution.

Cheers,
Paul

-- 
Paul Abbott                                   Phone: +61 8 9380 2734
School of Physics, M013                         Fax: +61 8 9380 1014
The University of Western Australia      (CRICOS Provider No 00126G)         
35 Stirling Highway
Crawley WA 6009                      mailto:paul at physics.uwa.edu.au 
AUSTRALIA                            http://physics.uwa.edu.au/~paul


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