I have been reading about how symbolic languages works, focusing more on Mathematica. As far as I could understand, to evaluate is to apply a sequence of transformation rules to the input until no more matching transformations rule could be found and call the result of it "output".
But then comes the problem: what to do when more than one of those transformation rule match a given expression? I tried this first example:
A[x_, 3] := 0;
A[x_, y_] := 1;
A[a, b]
=> 1
A[k, 3]
=> 0
I believe I can explain this by saying 3
matches 3
"better" than y
. Then my second experiment:
B[x_, 3] := 0;
B[4, y_] := 1;
B[4, 3]
=> 0
Why is this? I expected to see some kind of error.
The precidnece is simply by the order in which the functions were defined.
ClearAll[B]
B[x_, 3] := 0;
B[4, y_] := 1;
B[4, 3]
(* 0 *)
ClearAll[B]
B[4, y_] := 1;
B[x_, 3] := 0;
B[4, 3]
(* 1 *)
Watch out, things get confusing if you re-define functions..
ClearAll[B]
B[x_, 3] := 0;
B[4, y_] := 1;
B[4, y_] := 2;
B[x_, 3] := 3;
B[4, 3]
(* 3 *)
Notice the definitons are correctly changed but the order goes according to the original sequence. (hence liberal use of ClearAll while working with these sorts of things)
to see the order use:
??B