functional programming excercise from Mastering Mathematica

functional programming excercise from Mastering Mathematica

Post by Torsten Co » Fri, 18 Mar 2005 17:38:17


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
 
 
 

functional programming excercise from Mastering Mathematica

Post by Jens-Peer » Sat, 19 Mar 2005 19:45:23

Hi,
foldList[f_, arg_, lst_] :=
ComposeList[Function[{x}, f[#, x] &] /@ lst, x]

??

Regards

Jens



"Torsten Coym"
< XXXX@XXXXX.COM > schrieb im

 
 
 

functional programming excercise from Mastering Mathematica

Post by dh » Sat, 19 Mar 2005 19:46:09

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
 
 
 

functional programming excercise from Mastering Mathematica

Post by Andrzej Ko » Sat, 19 Mar 2005 19:49:11


foldList[f_, x_, l_List] := ComposeList[Map[Function[z, f[z, #]] &, l],
x]



Andrzej Kozlowski
Chiba, Japan
http://www.yqcomputer.com/
http://www.yqcomputer.com/ ~akoz/
 
 
 

functional programming excercise from Mastering Mathematica

Post by Jon Harro » Sat, 19 Mar 2005 19:49:57


I get:

foldlist[f_, x_, l_List] :=
ComposeList[Function[{a}, Function[{x}, f[x, a]]] /@ l, x]

--
Dr Jon D Harrop, Flying Frog Consultancy
http://www.yqcomputer.com/
 
 
 

functional programming excercise from Mastering Mathematica

Post by Wolf, Hart » Sat, 19 Mar 2005 19:53:45


Torsten,
as...

In[3]:= FoldList[f, x, {a1, a2, a3, a4}]
Out[3]=
{x, f[x, a1], f[f[x, a1], a2], f[f[f[x, a1], a2], a3],
f[f[f[f[x, a1], a2], a3], a4]}


...is

In[5]:=
ComposeList[{f[#, a1] &, f[#, a2] &, f[#, a3] &, f[#, a4] &}, x]
Out[5]=
{x, f[x, a1], f[f[x, a1], a2], f[f[f[x, a1], a2], a3],
f[f[f[f[x, a1], a2], a3], a4]}

we just have to build the first element in ComposeList from f and {a1,
a2, a3, a4}.



Several ways exist, e.g.

In[35]:= Function[e, f[#, e] &] /@ {a1, a2, a3, a4}
Out[35]=
{f[#1, a1] &, f[#1, a2] &, f[#1, a3] &, f[#1, a4] &}

In[36]:= Function[z, f[z, #]] & /@ {a1, a2, a3, a4}
Out[36]=
{Function[z, f[z, a1]], Function[z, f[z, a2]], Function[z, f[z, a3]],
Function[z, f[z, a4]]}

In[37]:= Function /@ Thread[f[#, {a1, a2, a3, a4}]]
Out[37]=
{f[#1, a1] &, f[#1, a2] &, f[#1, a3] &, f[#1, a4] &}

In[38]:=
Thread[Unevaluated[Composition[Function, f][#, {a1, a2, a3, a4}]]]
Out[38]=
{f[#1, a1] &, f[#1, a2] &, f[#1, a3] &, f[#1, a4] &}
 
 
 

functional programming excercise from Mastering Mathematica

Post by Valeri Ast » Sat, 19 Mar 2005 19:55:16

Why not this way :

In[1]:=
foldList[f_,x_,vars_]:=ComposeList[Function[x,f[x,#]]& /@ vars,x]

In[2]:=
foldList[f, x, {a, b, c }]

Out[2]=
{x,f[x,a],f[f[x,a],b],f[f[f[x,a],b],c]}

In[3]:=
FoldList[f, x, {a, b, c }]

Out[3]=
{x,f[x,a],f[f[x,a],b],f[f[f[x,a],b],c]}


v.a.
 
 
 

functional programming excercise from Mastering Mathematica

Post by Peter Pei » Sat, 19 Mar 2005 20:01:20


foldList[f_, x_, {}] := {x};
foldList[f_, x_, lst_List] :=
{x, Sequence @@ foldList[f, f[x, First[lst]], Rest[lst]]};

foldList[f, x, {a, b, c}] == FoldList[f, x, {a, b, c}]

==> True

If you need to handle large Lists (length > 250), you'll reach the
recursion limit.
You could try the procedural approach

Clear@foldList;
foldList[f_, x_, lst_] :=
Module[{n = Length[lst], r},
For[i = 1; r = Table[x, {n + 1}], i <= n, i++,
r[[i + 1]] = f[ r[[i]], lst[[i]] ]
];
r];

or even better the builtin FoldList ;-)
--
Peter Pein
Berlin
 
 
 

functional programming excercise from Mastering Mathematica

Post by Luis Radem » Sat, 19 Mar 2005 20:09:40

fl[f_, x_, a_List] := ComposeList[Map[(Function[y, f[y, #]]) &, a], x]

Luis.