I am trying to use a Module
having "local functions", i.e., those which I need to define only inside this module.
So I tried this:
norm[p_] := Module[{
fun1[p_] := p^2 + p - 1;
fun2[p_] := p^3 - p^2 + p + 1
},
Max[fun1[p], fun2[p]]
];
The function is compiling, but when I try to evaluate it--say, I try:
norm[2]
Its giving me an error telling:
Module::lvsym
: Local variable specification{fun1[p_]:=p^2+p-1;fun2[p_]:=p^3-p^2+p+1}
containsfun1[p_]:=p^2+p-1;fun2[p_]:=p^3-p^2+p+1
, which is not a symbol or an assignment to a symbol
How do we avoid this error? I want to give functions in the space between { ... }.
Answer
You cannot make definitions with patterns on the left-hand side in the first argument of a scoping construct (such as Module
). You need do that in the body of the Module
. You should also use a different symbol for the internal function parameter.
norm[x_] :=
Module[{fun1, fun2},
fun1[p_] := p^2 + p - 1;
fun2[p_] := p^3 - p^2 + p + 1;
Max[fun1[x], fun2[x]]
];
Closely related:
Comments
Post a Comment