2

I would like to define a function within a function to serve as an auxiliary function. In essence, something like this:

f[x_,y_]:=Module[{g[z_]:=2z},x+g[y]]

Where both f and g are in reality complicated functions. But this does not work because Module, or With, do not accept rule delayed in their definitions.

f[1,2]
Module::lvset: Local variable specification {g[z_]:=2 z} contains g[z_]:=2 z, which is an assignment to g[z_]; only assignments to symbols are allowed.

Is there a way around it?

Whelp
  • 1,715
  • 10
  • 21

1 Answers1

4

If you want, you can use

f[x_, y_] := Module[{g}, g[z_] := 2 z; x + g[y]]
Kiro
  • 1,521
  • 8
  • 19
  • How to increase reputation fast: answer the easy questions before they get marked as duplicates :p – Kiro Jun 08 '17 at 11:35