Programmatically creating multivariate functions in Mathematica

前端 未结 3 1793
别那么骄傲
别那么骄傲 2021-01-24 10:29

This is a split from discussion on earlier question.

Suppose I need to define a function f which checks if given labeling of a graph is a proper coloring. In other words

3条回答
  •  情话喂你
    2021-01-24 11:00

    There are a couple other solutions to this kind of problem which don't require you to use Hold or ReleaseHold, but instead rely on the fact that Function already has the HoldAll attribute. You first locally "erase" the definitions of Part using Block, so the expression you're interested in can be safely constructed, and then uses With to interpolate that into a Function which can then be safely returned outside of the Block, and also uses the fact that Slot doesn't really mean anything outside of Function.

    Using your example:

    coloringChecker[edges_List] := 
     Block[{Part},
      With[{body = And @@ Table[#[[First@edge]] != #[[Last@edge]], {edge, edges}]},
       body &]]
    

    I don't know if this is all that much less cumbersome than using Hold, but it is different.

提交回复
热议问题