1
$\begingroup$

If I have an arbitrary f[x, y, z], how do I convert it to f[#1, #2, #3] & without messing up scopes or causing scope leakage?

Note: I want to generalize this into a generic form, e.g. MakeFunction[exp_, args__].

$\endgroup$

1 Answer 1

1
$\begingroup$

It turns out that this is deceptively simple:

MakeFunction[exp_, args___] = Function[{args}, exp]; SetAttributes[MakeFunction, HoldFirst];

Now if you say:

MakeFunction[Sin[x + y], x, y];

You get:

Function[{x, y}, Sin[x + y]]
$\endgroup$

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.