1
$\begingroup$

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

The idea is as follows:

Funccall[a_,b_,n_]:=
  For[i=1,i<=n,i++,{Return[i*(4+i/3)*Funccall[a,b,n-1];}

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

Edit

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.

$\endgroup$
6
  • 2
    $\begingroup$ See here. $\endgroup$
    – corey979
    Commented Apr 6, 2018 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
    Commented Apr 6, 2018 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$ Commented Apr 6, 2018 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$ Commented Apr 6, 2018 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
    Commented Apr 6, 2018 at 19:07

1 Answer 1

0
$\begingroup$

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
      depth++
      if (depth == MaxDepth)    
       return

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

    }
$\endgroup$

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