Consider the following MWE
Clear["Global`*"]
randFunc[listFunc_] := RandomChoice[listFunc]@x;
mwe[list_] := Module[{testfun1, testfun2},
testfun1[x_] = RandomChoice[list]@x;
testfun2[x_] = randFunc[list];
N@{testfun1[2], testfun2[2]}
];
mwe[{Sqrt, Log}]
Which gives output that looks like {0.693147, Log[x]}
The numbers/function changes because of RandomChoice
, but why does testfun1[2] evaluate but not testfun2[2]?
Note: The following code (where the list is explicitly input in testfun1
and testfun2
does work
Clear["Global`*"]
randFunc[listFunc_] := RandomChoice[listFunc]@x;
testfun1[x_, list_] = RandomChoice[list]@x;
testfun2[x_, list_] = randFunc[list];
N@{testfun1[2, {Sqrt, Log}], testfun2[2, {Sqrt, Log}]}
Which gives output like {1.41421, 0.693147}
(again, RandomChoice
can change values, but they are both evaluating)
Note 2: I can get the expected behavior in the first example by changing testfun2[x_] = randFunc[list] ;
to testfun2[y_] = randFunc[list] /. x -> y;
, but I don't understand why I would need to do this.
Looking at the trace, Maybe randFunc[list]
is not evaluating when testfun2[x_]
is being defined in the Module
, but I don't understand why this would be?
- (maybe the
HoldAll
attribute ofModule
?)