Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.8K Views
|
1 Reply
|
1 Total Like
View groups...
Share
Share this post:

Pass a variable to a recursive function?

Posted 7 years ago

I am trying to modify the following recursive function.

EX[n_] := EX[n] = EX[n - 1]*x^n
EX[1] := x

This code works and provides output such as.

In[194]:= EX[7]

Out[194]= x^28

What I would like is a function that looks like this.

EX[x_,n_] := EX[x,n] = EX[n - 1]*x^n
EX[1] := x

So that;

EX[y,15]

results in,

 y^15

Obviously, the code above does not work. Any help is greatly appreciated.

POSTED BY: Edward Davis

You got it almost right:

EX[x_, n_Integer] := EX[x, n] = EX[x, n - 1]*x^n
EX[x_, 1] := x

If you have version 11.3 you can use the new Curry to play around with the order of variables:

Curry[EX][15][x]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard