Re: functional programming excercise from Mastering Mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg55270] Re: functional programming excercise from Mastering Mathematica
- From: dh <dh at metrohm.ch>
- Date: Fri, 18 Mar 2005 05:33:47 -0500 (EST)
- References: <d1bflp$lja$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hello Torsten,
Note that ComposeList takes a function with 1 argument however FoldList
a function with 2 arguments.
To mimic FoldList using ComposeList, we must first reduce the two
argument function to a one argument function by fixing the second
argument. This can be done by:
Function /@ Thread[fun,{a,b,c}]
Therefore, we may define:
foldList[fun_,x_,par_List]:=Module[{funs},
funs= Function /@ Thread[fun[#, par]];
ComposeList[funs,x]
]
To make it short and cryptic, we can write a one liner:
foldList[fun_,x_,par_]:=ComposeList[Function /@ Thread[fun[#, par]], x]
Daniel
Torsten Coym wrote:
> I'm quite new to Mathematica and its functional programming capabilities
> so I did some reading in John Gray's "Mastering Mathematica". There is
> an excercise in chapter 6 as follows
>
> Write your own function composeList that works just like the built-in
> operation with the same name, using FoldList. Conversely, write your own
> function foldList that works just like the built-in operation with the
> same name, using ComposeList.
>
> Unfortunately, there is no solution given at the end of the book (or I
> didn't find it). I could figure out a way to do the first task:
>
> composeList[funlist_List, var_] := FoldList[(#2[#1]) &, var, funlist]
>
> but I can't manage the second task...
>
> I know it's rather academic, but ... any help is welcome!
>
> Torsten
>