Defining a function with a computed definition



A couple of recent queries (Madhusudan Singh; Robert Dodier) ask how to
make a formula that you've calculated into a function definition.  This
certainly belongs in the FAQ.

-------------------------------

Q: How do I use an expression I've calculated using Maxima as the
definition of a Maxima function?  For example, given

    expr: logcontract(integrate(2/(x^2-1),x))

I want to define

    f(x) := log((x-1)/(x+1));

without re-entering the right-hand side by hand?


A: There are two (main) ways:

1) Use the ''(...) operator.  ''(...) substitutes the value of ... in
the expression as it is being read.  So:

    f(x):= ''(expr);

The parentheses are not necessary in this case, but I prefer to use them
for clarity and consistency.  Note that '' is two single-quotes, not one
double-quote.

2) Use the Define function.  Define allows both the function to be
defined and the definition to be calculated:

    define('(f(x)),expr);

I have quoted f(x).  This is only strictly necessary if f has a previous
definition, but I think it is good practice to quote explicitly.

      -s

----------------

Funmake (which Dan Stanger mentions) is only necessary in some very
special cases of using Define.  So special that I think it would not be
useful to discuss them.