0
$\begingroup$

I am trying to define a variable and use it inside a function. However, I am having troubles calling the variable. For instance, I define the function

interSpecialCycle[permutation_]:=PermutationCycles[permutation][[1]][[1]]

and I want to assign that function to a variable of the form

list1 = SpecialCycle = interSpecialCycle[permutation]

I want to use these variables in different functions I want to write, but when I call list1 nothing happens.

Is there a way to do this in mathematica?

$\endgroup$
1
$\begingroup$

This works fine:

interSpecialCycle[permutation_] := 
 PermutationCycles[permutation][[1]][[1]]
myperm = {1, 4, 2, 5, 3}
list1 = SpecialCycle = interSpecialCycle[myperm]

But list1 will not be callable, so I don't know what you mean by that: interSpecialCycle[myperm] returns a list. Are you saying that you currently call it several times with the same argument? Then just store the result in a variable. It sound like you want that variable to be local to your larger function. If that's the case, use Module:

myfunc[input_List]:=Module[{list1=interSpecialCycle[input]},
  <function body referring to list1>]
$\endgroup$
  • $\begingroup$ So I am writing a big function and I will call several times SpecialCycle and so on, but the problem with list1 = SpecialCycle = interSpecialCycle[myperm] is that I need to change myperm everytime I use a different list and I am trying to do it a bit more automatic by just changing the list once in the whole program. $\endgroup$ – user41116 Jun 19 '16 at 17:48
  • $\begingroup$ @user41116 This is still unclear. interSpecialCycle[myperm] returns a list. Are you saying that you currently call it several times with the same argument? Then just store the result in a variable -- presumably a variable that is local to your large function. I'll add an example. $\endgroup$ – Alan Jun 19 '16 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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