mpettis
mpettis

Reputation: 3329

Convert an expression to a function

If I have a named expression, how can I convert that to a function? Here's an example:

argx: 2*x;
f(x) := argx;

I'd like then this to be equivalent to:

f(x) := 2*x

But I know somehow I have to force "unquoting" or something of argx

Upvotes: 1

Views: 475

Answers (1)

Robert Dodier
Robert Dodier

Reputation: 17576

There are a couple of ways to do that.

(1) define(f(x), argx);

(2) f(x) := ''argx;

Approach (2) only works when you're in the top-level interpreter, since quote-quote '' (i.e., two single quotes) is only applied when an expression is first parsed. Approach (1) works within functions, while (2) does not, so (1) is a little more general. However, since function definitions are global, there isn't much motivation to define named functions within functions.

Upvotes: 4

Related Questions