0
$\begingroup$

I'm pretty sure that analogous questions have been asked here a zillion of times, but...

I think it is pretty straightforward from the code what I expect it to give:

oplist[] := Module[{op, list = {}, k},
   For[k = 1, k <= 3, k++,
    AppendTo[list, Function[x, x^k]];
    ];
   For[k = 4, k <= 5, k++,
    AppendTo[list, Function[x, x^k]];
    ];
   Return[list];
   ];
list = oplist[];
For[k = 1, k <= Length[list], k++,
  Print[list[[k]][a]];
 ];

Instead of a, a^2, a^3... I have a^6, a^6, a^6... I guess, it's something about pointers/links/evaluating at the moment when smth is called.

I tried replacing x^k with x^Evaluate[k], but this does not help either.

$\endgroup$
2
  • 2
    $\begingroup$ Try this: f[x_] := x^Range[5] This gives a list of the functions you appear to want. If you want t evaluate at a, then f[a] gives your list {a, a^2, a^3, a^4, a^5}. $\endgroup$
    – bill s
    Nov 1, 2018 at 21:50
  • 1
    $\begingroup$ Or if you need them to be operators then oList = With[{k = #}, #^k &] & /@ Range@5 which can then be invoked with Through@oList@a . $\endgroup$
    – Edmund
    Nov 1, 2018 at 22:08

2 Answers 2

1
$\begingroup$
Attributes[Function]

{HoldAll, Protected}

That is, all arguments to Function are to be maintained in an unevaluated form. So x^k in Function[x, x^k] is not evaluated unless it is wrapped with Evaluate:

ClearAll[k, x, a, oplist]
oplist[] :=  Module[{op, list = {}, k}, 
   For[k = 1, k <= 3, k++, 
    AppendTo[list, Function[x, Evaluate[x^k]]];];
   For[k = 4, k <= 5, k++, 
    AppendTo[list, Function[x, Evaluate[x^k]]];];
   Return[list];];

list = oplist[];
For[k = 1, k <= Length[list], k++, Print[list[[k]][a]];];

enter image description here

Alternatively, use With[{j = k}, AppendTo[list, Function[x, x^j]]] in place of AppendTo[list, Function[x, Evaluate[x^k]]].

$\endgroup$
0
$\begingroup$

just remove k from

 Module[{op, list = {}, k}

this will get it right. no other changes in your code

oplist[] := 
Module[{op, list = {}}, 
For[k = 1, k <= 3, k++, AppendTo[list, Function[x, x^k]]];
For[k = 4, k <= 5, k++, AppendTo[list, Function[x, x^k]]];
Return[list]]
list = oplist[]
For[k = 1, k <= Length[list], k++, Print[list[[k]][a]];];
$\endgroup$
4
  • $\begingroup$ Could you please explain why it happens like that? In general, should I never list cycle indices inside the list of local objects? Will they not interfere with the indices defined outside of the Module? $\endgroup$
    – mavzolej
    Nov 1, 2018 at 22:05
  • 1
    $\begingroup$ k is defined inside For... Why do you use 2 For-loops instead of one? $\endgroup$
    – ZaMoC
    Nov 1, 2018 at 22:10
  • $\begingroup$ The question arises from a more general context, where different cycles inside Module cannot be combined into one, and the outcome of the procedure so far depends on order of cycles inside it. $\endgroup$
    – mavzolej
    Nov 1, 2018 at 22:11
  • $\begingroup$ Please see my question here. $\endgroup$
    – mavzolej
    Nov 1, 2018 at 23:19

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.