Mathematica is, typically, be seen as a calculator. But it do has some shine feature.
Take this as an example:
is it ? yes, but what defines a function? Do we miss something? Oh, the domain!
so it can be in , but you know, in real world, there aren’t just number, we have more things other than that. For example,
String
.get what? oh,
"12"^2
, what’s what? So why does this happen? Because the pattern
Blank[]
, aka _
in x_
can accept anything. And mma will hold the expression. (calculate a[b]
and mma will return itself.So how do we know the type? In mma, we have
Head
, Since mma store the 0th element as it’s type. And we can match it with Blank[]
Typed based overload.
But it’s much powerful than that.
we can double anything!
:=
because StringJoin
need to calculate the expression at once. So we need to delay it.But if I want to double any number?
notice the difference betweenNumericQ
andNumberQ
,NumberQ[Sqrt[2]]
return False.
in some aspect, we can think_?NumericQ
as aProtocal
orInterface
orTrait
.(For example,num-traits
in rust.
So, if you want to match a/some certain type(s), use
_X
or _X|_Y
, else use _?XQ
.That is to say, we want a method to check type meets some conditions.
in rust, we use
impl trait
or where
or dyn trait
in cpp, use sfinae(I don’t know it.) or concept. (Emit)
TODO:
implement this in mma
Since mma can
Hold
value by default, it can be seem as oop
.