Puzzled by Function body evaluation

半世苍凉 提交于 2019-12-14 01:58:42

问题


I am puzzled by the following behavior of Function:

In[1]:= InlineCellInMessage=Function[expr,DisplayForm[Cell[BoxData[MakeBoxes[expr,StandardForm]],"Input"]],{HoldAllComplete}]
Out[1]= Function[expr,MakeBoxes[expr,StandardForm]]

I expected to see unevaluated code inside Function in the output as in the following case:

In[2]:= InlineCellInMessage=Function[x,x+1+1]
Out[2]= Function[x,x+1+1]

But I get the inline cell inside output. Why does this happen?


回答1:


This is the result of FrontEnd rendering. Consider:

InlineCellInMessage = 
  Function[expr,DisplayForm[Cell[BoxData[MakeBoxes[expr,StandardForm]],"Input"]],{HoldAllComplete}]

InlineCellInMessage // InputForm

Output:

InputForm[Function[expr, DisplayForm[Cell[BoxData[MakeBoxes[expr, StandardForm]], "Input"]], {HoldAll.Complete}]]

Also, in this use the parameter HoldAllComplete affects future input to the function, not the creation of the function itself. If you want Function itself to have HoldAllComplete you need:

SetAttributes[Function, HoldAllComplete]


来源:https://stackoverflow.com/questions/5616305/puzzled-by-function-body-evaluation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!