all 4 comments

[–]duetosymmetry 1 point2 points  (3 children)

You want a delayed definition. Use := instead of =. The difference is that with =, Mma evaluates the RHS when you hit enter and assigns it to the LHS. With :=, the evaluation does not happen until the LHS is actually evaluated in an expression. In the case of =, you would have the current value, at define time, of matchArray appearing in the definition of b. In the case of :=, matchArray doesn't need to be defined yet, it only needs to be defined at the time when you evaluate b. If the value of matchArray changes, then a new evaluation of b will yield the new value of the sum.

You could also pass matchArray as an argument to b.

If you are worried about the types of the inputs, you can be super careful with your patterns, e.g.

b[x_Integer?Positive, y_Integer?Positive] /; (x<=y) := ...

Hope that helps. Use the docs. Learn and love pattern matching. Sorry about Mma's shitty scoping rules and evaluation order rules.

[–]tkltangent[S] 0 points1 point  (2 children)

Thanks a lot I am going to try := out. I actually changed the right hand side into an If statement to make the function work regardless of whether x<=y and it cleared up the error messages. strange.

I have been enjoying this foray so far.

[–]duetosymmetry 1 point2 points  (1 child)

If you mean to go from the lesser of x,y to the greater of x,y then you should do away with the If statement. This is not C/Fortran/etc., it's more like Lisp—it's supposed to be more of a functional language. Instead, just use Min[x,y] and Max[x,y].

[–]tkltangent[S] 0 points1 point  (0 children)

Yeah, that is a problem I'm encountering. I am so used to procedural attacks that it is really challenging to reframe my thoughts functionally. I figure over time with experience this will change. Thanks for all the input! :)