【〇疼】让Mathematica支持do…end语法(雾):TemplateBox介绍
mathematica吧
全部回复
仅看楼主
吧务
level 15
xzcyr 楼主
受某帖启发写了个无聊的东西:
本来想多写点内容介绍下具体思想的,但是现在突然觉得累了,所以算了……其实我对这项技术也不算很熟悉啦。总之Mathematica里各种看似不符合基本语法的马甲
或许就是用同样的方法造出来的。此外,Stackexchange的Carl Woll(Wolfram公司员工,Wolfram|Alpha的首席开发者)是使用此项技术的高手,有兴趣的可以多看看他的帖子。
总之最后是源代码:
ClearAll@do
SetAttributes[do, HoldAll];
do[var_, begin_, end_, body_] := Do[body, {var, begin, end}]
Hold[do /: MakeBoxes[do[var_, begin_, end_, body_], StandardForm] :=
TemplateBox[MakeBoxes /@ Unevaluated@{var, begin, end, body}, "do",
DisplayFunction -> (GridBox[{{RowBox@{RowBox[{style@"do", " ", #}], "=",
RowBox[{#2, " ", style@"to", " ",
#3}]}}, {RowBox@{"", #
4}}, {style@
"end"}}, ColumnAlignments -> Left] &)]] /.
style[str_] -> StyleBox[str, FontColor -> Darker@Green] // ReleaseHold // ReleaseHold
Hold[CurrentValue[EvaluationNotebook[], {InputAliases, "do"}] =
TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]", "\[Placeholder]",
"\[Placeholder]"}, "do",
DisplayFunction -> (GridBox[{{RowBox@{RowBox[{style@"do", " ", #}], "=",
RowBox[{#2, " ", style@"to", " ",
#3}]}}, {RowBox@{"", #
4}}, {style@
"end"}}, ColumnAlignments -> Left] &)]] /.
style[str_] -> StyleBox[str, FontColor -> Darker@Green] // ReleaseHold
2019年01月05日 12点01分 1
level 4
Mathematica是图灵完备的,对于大神真的是无所不能。
2020年07月24日 03点07分 4
1