I want to create a function (or anything, really) that is capable of calling its own logic.

The idea is as follows:


or something like that. Not the exact function; it's too long. How do i achieve this effect?


Here's the purpose of the code: to generate all products from a matrix from elements which dont share rows or columns

For[i in n, for j in n, for k in in [This goes on till n times i.e. n for loops]
  if j!=i && k!=i &&k!=j &&.....->Multiply[a1i,a2j,a3k,a4l..........]

I need a function to do this.

The If condition I've implemented by creating an empty array used that stores whatever numbers have been used, and then I use FreeQ[used, k].

I hope this gives some clarity.


closed as unclear what you're asking by m_goldberg, Henrik Schumacher, Young, José Antonio Díaz Navas, J. M. will be back soon Apr 10 '18 at 22:23

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    $\begingroup$ See here. $\endgroup$ – corey979 Apr 6 '18 at 18:50
  • 4
    $\begingroup$ f[0] = 1; f[x_] := x f[x - 1]; Factorial implemented recursively. Generally using For and Return are not the best use of Mathematica's capabilities. This may be an X-Y problem, what are you actually trying to accomplish? $\endgroup$ – eyorble Apr 6 '18 at 18:55
  • $\begingroup$ @corey979 thanks i think that could help. The problem is that my parameters will also change inside the nesting. WIll have to find a way to work around that $\endgroup$ – Ashwin Kumar Apr 6 '18 at 18:58
  • $\begingroup$ @eyorble im trying to implement a product rule, that multiples elements only when they are not from the same row or column. Its a sparse matrix, so dont want to do this for all elements $\endgroup$ – Ashwin Kumar Apr 6 '18 at 18:59
  • 4
    $\begingroup$ That sounds like a far more interesting question than how to implement recursive functions in Mathematica, so if you're willing to write up the general idea of what you're trying to accomplish there and a minimal working example with what you already have, it'd probably be more helpful for you. $\endgroup$ – eyorble Apr 6 '18 at 19:07

In a simple pseudo code this is a simple Recursive method to visit all nodes in a tree and set the X,Y to known values. It will unwind itself when the maximum depth has been reached. This might help you with some logic. Sorry its not your native format.

    To initialise:
    Call Recurse(RootNode, 1, 1, 0, 20)

    Recurse(TreeNode node, int X, int Y, int depth, int MaxDepth) {

      node.X = X
      node.Y = Y
      if (depth == MaxDepth)    

      TreeNode nextNode = node.NextNode
      Recurse(nextNode, X, Y, depth, MaxDepth)


Not the answer you're looking for? Browse other questions tagged or ask your own question.