Message Boards Message Boards

0
|
3007 Views
|
3 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Concatenating a function

Posted 12 years ago
Hello everyone,

I need to generate many functions of the same family; A much simplified version of which is something like:

f"i"[x_] =: x+i.

So I want to name the different functions like

f1 [x_] = x+1
f2 [x_] = x+2
...
f50 [x_] = x+50
...
fi [x_] = x+i

Any suggestions on how to create them with the name I want to;
3 Replies
Thank you both. I used your suggestion Sean!
It is possible to do this, but I strongly recommend against it. Most often, this kind of programming leads to bad design and is difficult to debug. Shenghui's suggestion is the best way to do this kind of thing.

To answer your question, you can define your functions all at once by evaluating:
Do[Symbol["f" <> ToString[i]][x_] := Evaluate[x + i], {i, 1, 50}]
POSTED BY: Sean Clarke
You can easity do this by either defining
f[x_,i_]:= x+i
orĀ 
f[i_][x_]:=x+i
The latter is an example of how to use Operate function.
POSTED BY: Shenghui Yang
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract