Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I can't figure out how to use fold/table/map/distribute or whatever to get this:

{f[f[{},a],a],f[f[{},a],b],f[f[{},a],c],f[f[{},a],d],f[f[{},a],e],
f[f[{},b],a],f[f[{},b],b],f[f[{},b],c],f[f[{},b],d],f[f[{},b],e],
f[f[{},c],a],f[f[{},c],b],f[f[{},c],c],f[f[{},c],d],f[f[{},c],e],
f[f[{},d],a],f[f[{},d],b],f[f[{},d],c],f[f[{},d],d],f[f[{},d],e],
f[f[{},e],a],f[f[{},e],b],f[f[{},e],c],f[f[{},e],d],f[f[{},e],e]}

That is I need to evalute f[{},n] n=a,...,e. For each result I need do the same again and again. So total evaluations for i iterations should be:

Sum[Power[5,n],{n,i}]
share|improve this question
    
You are right :) sorry. –  Kuba Nov 30 '13 at 9:41

1 Answer 1

up vote 2 down vote accepted
L = {a,b,c,d,e};
f @@@ Tuples@{f[{},#]&/@L, L}

does 5 + 25 evaluations and gives

{f[f[{},a],a],f[f[{},a],b],f[f[{},a],c],f[f[{},a],d],f[f[{},a],e],
 f[f[{},b],a],f[f[{},b],b],f[f[{},b],c],f[f[{},b],d],f[f[{},b],e],
 f[f[{},c],a],f[f[{},c],b],f[f[{},c],c],f[f[{},c],d],f[f[{},c],e],
 f[f[{},d],a],f[f[{},d],b],f[f[{},d],c],f[f[{},d],d],f[f[{},d],e],
 f[f[{},e],a],f[f[{},e],b],f[f[{},e],c],f[f[{},e],d],f[f[{},e],e]}
share|improve this answer
    
Nest[f @@@ Tuples@{#, L} &, f[{}, #] & /@ L, i - 1] is 30% faster than my own code thank you :D –  Coolwater Nov 30 '13 at 9:14
    
Nest[f @@@ Tuples@{#, test} &, {{}}, i] even better –  Coolwater Nov 30 '13 at 9:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.