It seems to me that functions defined using a simple pattern and SetDelayed
(the usual way to define a "function") should be equivalent in all respects to functions defined using Function
: both say what to do with a value provided in a slot, and merely have different methods of designating the slot. But they are not. Observe:
f1[x_] := x^2;
f2 = Function[#^2];
{f1[3], f2[3]}
(* {9, 9} *)
So far, so good. However:
g[ff_Function, y_] := ff[y];
{g[f1, 3], g[f2, 3]}
(* {g[f1, 3], 9} *)
One can be passed around via its symbol and be invoked as a callback, and the other cannot. It would be convenient (and, for the novice programmers I teach, less confusing) to define functions for callbacks with the more transparent SetDelayed notation, rather than with pure function notation.
Clearly, there is some deep difference between these two rules that I do not understand. Perhaps someone wiser than I can illuminate?
_Function
in your first argument ing
, you only allow the first argument to be aFunction
- otherwise the definitiion does not apply. Useg[ff_, y_]:=...
, and you get what you want in both cases. $\endgroup$