I want to program a code in MATHEMATICA in order to calculate some values in different iteration. For example, assume this list:


Now assume that the function is :


I want to use a Do loop to orderly put each integer from the list into f1 function and Do loop uses 5 iteration ({i,1,5}) and gives 5 answers using the values in the list. For this example, The results will be :

4 5 6 7 8

I wonder if anyone could help me with this problem.

  • $\begingroup$ My guess is that you have tried something along the lines of Do[f1[list[[x]]],{x,5}] and that you got nothing as output. Try telling it to Print the output by wrapping the f1[list[[x]]] with Print[]. The answer that Christopher Cole provided using Map is clearly a better way to solve this. $\endgroup$ Mar 30, 2014 at 15:46

1 Answer 1


I think you want to define f1 as

f1 = Function[u, 3 + u]

since the form you started with would have created the function and then applied it to x.

The easiest way to do what you want is to use Map:

Map[f1, list]

which has an equivalent syntax

f1 /@ list

If you really must use a Do loop, you would do it as follows:

Module[{result = {}}, Do[AppendTo[result, f1[i]], {i, list}]; result]

This is a lot more work. The Module that wraps the Do loop creates a result symbol to hold your output. The Do loop iterates over the elements of list, appending the computation of f1 on each element to the result output.

  • $\begingroup$ @ Christopher Thank you so much!! $\endgroup$
    – Shellp
    Mar 31, 2014 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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