Skip to main content
formatted code
Source Link
rcollyer
  • 34.1k
  • 7
  • 94
  • 194

I can't figure out what's wrong with this piece of code. I'm trying to define a function that counts the occurrences of xx, yy and ll or lclc in expressions like x[x[y[l]]x[x[y[l]], where xx will add 1 to an "eigenvalue", yy will subtract 1 and likewise for ll and lclc. Then for example I would get H[x[x[y[l]]] = 2H[x[x[y[l]]] = 2. Now I'm stuck at the first phase with the function defined below:

 H[z_] := Module[{list1 = Join[Level[z, Infinity], {z}], lam = 0},
  Which[list1[[1]] == l, lam = lam + 1, 
        list1[[1]] == lc, lam = lam - 1];1
  lam]];
  lam
]

This should give H[l] = 1H[l] = 1 and H[lc] = -1H[lc] = -1, but instead I get just the initial value lam=0lam = 0 for the "lc" case... When I discard the last line and use instead

H[z_] := Module[{list1 = Join[Level[z, Infinity], {z}], lam = 0},
  Which[list1[[1]] == l, lam = lam + 1, 
        list1[[1]] == lc, lam = lam - 1]]1
  ]
]

I get

Which[lc == l, lam$538474 = lam$538474 + 1, 
      list1$538474[[1]] == lc, 
 lam$$538474[[1]] == lc, lam$538474 = lam$538474 - 1]1
]

Clearly something's f'd upwrong with the "list1$538474[[1]]"list1$538474[[1]] above... orWhat am I just tired and stupidmissing?!

I can't figure out what's wrong with this piece of code. I'm trying to define a function that counts the occurrences of x, y and l or lc in expressions like x[x[y[l]], where x will add 1 to an "eigenvalue", y will subtract 1 and likewise for l and lc. Then for example I would get H[x[x[y[l]]] = 2. Now I'm stuck at the first phase with the function defined below:

 H[z_] := Module[{list1 = Join[Level[z, Infinity], {z}], lam = 0},
  Which[list1[[1]] == l, lam = lam + 1, 
   list1[[1]] == lc, lam = lam - 1];
  lam]

This should give H[l] = 1 and H[lc] = -1, but instead I get just the initial value lam=0 for the "lc" case... When I discard the last line and use instead

H[z_] := Module[{list1 = Join[Level[z, Infinity], {z}], lam = 0},
  Which[list1[[1]] == l, lam = lam + 1, 
   list1[[1]] == lc, lam = lam - 1]]

I get

Which[lc == l, lam$538474 = lam$538474 + 1, list1$538474[[1]] == lc, 
 lam$538474 = lam$538474 - 1]

Clearly something's f'd up with the "list1$538474[[1]]" above... or am I just tired and stupid?!

I can't figure out what's wrong with this piece of code. I'm trying to define a function that counts the occurrences of x, y and l or lc in expressions like x[x[y[l]], where x will add 1 to an "eigenvalue", y will subtract 1 and likewise for l and lc. Then for example I would get H[x[x[y[l]]] = 2. Now I'm stuck at the first phase with the function defined below:

H[z_] := Module[{list1 = Join[Level[z, Infinity], {z}], lam = 0},
  Which[list1[[1]] == l, lam = lam + 1, 
        list1[[1]] == lc, lam = lam - 1
  ];
  lam
]

This should give H[l] = 1 and H[lc] = -1, but instead I get just the initial value lam = 0 for the "lc" case... When I discard the last line and use instead

H[z_] := Module[{list1 = Join[Level[z, Infinity], {z}], lam = 0},
  Which[list1[[1]] == l, lam = lam + 1, 
        list1[[1]] == lc, lam = lam - 1
  ]
]

I get

Which[lc == l, lam$538474 = lam$538474 + 1, 
      list1$538474[[1]] == lc, lam$538474 = lam$538474 - 1
]

Clearly something's wrong with the list1$538474[[1]] above. What am I missing?

Source Link

Rookie mistake in defining a function with Modules?

I can't figure out what's wrong with this piece of code. I'm trying to define a function that counts the occurrences of x, y and l or lc in expressions like x[x[y[l]], where x will add 1 to an "eigenvalue", y will subtract 1 and likewise for l and lc. Then for example I would get H[x[x[y[l]]] = 2. Now I'm stuck at the first phase with the function defined below:

 H[z_] := Module[{list1 = Join[Level[z, Infinity], {z}], lam = 0},
  Which[list1[[1]] == l, lam = lam + 1, 
   list1[[1]] == lc, lam = lam - 1];
  lam]

This should give H[l] = 1 and H[lc] = -1, but instead I get just the initial value lam=0 for the "lc" case... When I discard the last line and use instead

H[z_] := Module[{list1 = Join[Level[z, Infinity], {z}], lam = 0},
  Which[list1[[1]] == l, lam = lam + 1, 
   list1[[1]] == lc, lam = lam - 1]]

I get

Which[lc == l, lam$538474 = lam$538474 + 1, list1$538474[[1]] == lc, 
 lam$538474 = lam$538474 - 1]

Clearly something's f'd up with the "list1$538474[[1]]" above... or am I just tired and stupid?!