A wealth of knowledge is on display in MaplePrimes as our contributors share their expertise and step up to answer others’ queries. This post picks out one such response and further elucidates the answers to the posted question. I hope these explanations appeal to those of our readers who might not be familiar with the techniques embedded in the original responses.

**The Question: Transforming functions to names**

Bendesarts wanted to know how to make programmatic changes to characters in a list. He wrote:

I have this list :

T:=[alpha(t),beta(t)]

I would like to create this list automatically:

Tmod:=[alpha_,beta_]

In other words, how can I remove the 3 characters "(t)" and replace it by "_"

Do you have ideas to do so ?

Thanks a lot for your help

Joe Riel provided a complete answer that had three different approaches. He wrote:

Map, over the list, a procedure that extracts the name of the function and catenates an underscore to it. The function name is extracted via the op procedure, e.g. op(0,f(a,b,c)) evaluates to f. Thus

map(f->cat(op(0,f),_),T);

Note that this fails if the name of a function is indexed, e.g. f[2](a). No error is generated but the catenation doesn't evaluate to a symbol. Presumably that isn't an issue here. One way to handle that case is to first convert the indexed name to a symbol, then catenate the underscore. So a more robust version is

map(f->cat(convert(op(0,f),'symbol'),_),T);

However, if you are actually dealing with indexed names you might want a different result. Another way to do the conversion, and combine it with the catenation, is to use nprintf, which generates a name (symbol). Thus

map(f -> nprintf("%a_", op(0,f)),T);

Let’s discuss each approach by understanding the definitions and functionalities of the commands used.

The **map** command, **map(***fcn, expr, arg1, ..., argN***)** applies a procedure or name, *fcn*, to the operands or elements of an expression, *exp*r. The result of a call to map is a copy of *expr *with the ith operand of expr replaced by the result of applying *fcn* to the ith operand. This concept is easier to grasp by looking at a few examples related to the usage of map in this question.

Example 1. map(x-> x^{2},x+y) returns x^{2}+y^{2 }

Example 2. map(a -> a-b, sin(x)) returns sin(x-b)

The **cat** function**, cat(***a,b,c,…***)**, is commonly used to concatenate (or join) string and names together. This function’s parameters: a,b,c…, can be any expressions.

Example 1. cat(a,2) returns a2

Example 2. cat(“a”,3,sin(x)) returns “a3sin(x)”

The **op** function, **op(***i..j,e***)**, extracts operands from an expression. The parameters *i* and *j* are the integers indicating positions of the operands and *e* is the expression. For functions, as in this example, op(0,e) is the name of the function.

Example 1. op(0,alpha(t)) returns the symbol alpha

Example 2. op(0, sin(x)) returns sin

Now analyzing Joe Riel's code will be easier.

- map(f->cat(op(0,f),_),T);

In this approach Joe is extracting the name of the functions, alpha and beta, and then concatenating it to the underscore symbol. Then using the mapping function he applies the previous procedure to the list T.

- map(f->cat(convert(op(0,f),'symbol'),_),T);

This approach is a lot similar to the previous one, but he added the convert function in case the function inside of map was indexed. **Convert(***expr, form, arg3*,..**)**, is used to change an expression from one form to another. In this example op(0,f) has been changed from type name to type symbol.

- map(f -> nprintf("%a_", op(0,f)),T);

Again this is a similar approach but it uses nprintf. This command**, nprintf(***fmt,x1,..xn***)**, is based on a C standard library command of the same name. It uses the format specifications in the *fmt* string to format and writes the expression into a Maple symbol, which is returned. In this example the format specified is the algebraic format “%a”.

This blog was written by Maplesoft’s intern Pia under the supervision of Dr. Robert Lopez. We both hope that you find this useful. If there is a particular question on MaplePrimes that you would like further explained, please let us know.