Since we were discussing Apply
in another thread, consider coding it in Mathematica.
apply[f_, _[args___]] := f[args]
The SetDelayed
machinery rewrites this a bit, and puts it into the database of downvalues keyed by the head apply
. We can inspect this:
FullForm[DownValues[apply]
yields
List[RuleDelayed[HoldPattern[apply[Pattern[f,Blank[]],Blank[][Pattern[args,BlankNullSequence[]]]]],f[args]]]
All this pattern and blank stuff is really the foundation. Mathematica
's design revolves around mathematics, not programming. Suppose I have the expression:
a (b + c) == a b + a c
The left and right sides are different structurally, so ==
doesn't recognize the equality. But Mathematica
has functions that can rewrite this in a way that the two sides are identical, and thus further rewrite it to True
. Applying either Expand
or Factor
does this. Simplify
tries various rewrites until it finds one that leads to True
. Mathematica
is fundamentally a system for rewriting expressions, not programming. The language is designed to code rewrite rules. Expression rewriting can present a façade that looks like conventional programming, as in the apply
definition above. If you really want to understand Mathematica
from bottom up, though, you need to understand what things like RuleDelayed
and Pattern
represent. It's more like Unix sed than like an ordinary programming language.
I don't think implementing rewrite rules as Mathematica rewrite rules that imitate procedures that rewrite expressions is likely to be illuminating.