# How to define a function of many variable like f(x_1,x_2,...,x_n)?

I wanted to define a general function of many variables which can be used as substitution rule at many points. Here is an example:

rule:=f(???,??) = -???-??;
ex:=f(-a,-b);
substitute(ex,rule);

which gives $a+b$ and is correct. But, if I do

rule:=g(?,??,???) = -?-??-???;
ex:=g(-a,-b,-c);
substitute(ex,rule);

it gives $-a+b+c$ instead of $a+b+c$. The problem seems to be with using single ?. This disappears when I use ???? or ?????. For example

rule:=h(????,??,???) = -????-??-???;
ex:=h(-a,-b,-c);
substitute(ex,rule);

which gives correct result $a+b+c$.

Is there a general way to define such functions?

Thanks.

This triggers an interesting bug in the pattern matcher, thanks for pointing this out. In general, you are supposed to write this as

rule:= g(x??, y??, z??) = - x?? - y?? - z??;
ex:= g(-a,-b,-c);
substitute(ex, rule);

which gives $a+b+c$ as expected. This uses so-called 'object patterns', which are indicated by a name followed by two question marks. You then repeat that name and the question marks on the right-hand side of the substitution rule.

Now in your particular case things should have worked also with

rule:= g(x?, y?, z?) = -x? -y? -z?;    # triggers a bug

because a 'symbol pattern' like x? will match a (and so on). This somehow slipped the net (probably no-one wrote it in this form so far). Will put it on the todo list for the next release (in the meantime, you should be fine with the solution given earlier).

by (64.9k points)
selected by

Forgot to explain: the reason why it worked for you when you avoided single question marks is that

rule:= h(????,??,???) = -????-??-???;

has three object wildcards, one which has name '??', one which has an empty name, and one which has name '?'. This is allowed (as you can see, it works) but I would recommend using proper names for wildcards, not series of question marks.

Thanks a lot.