New answers tagged

4

It is because Attributes have UpValues(*) associated with them so at the end you are not trying to Set to Attributes[Foo] but it will be translated. You can mimic that with e.g. UpSetDelayed (^:=): ClearAll[f] Set[f[x_], attr_] ^:= SetAttributes[x, attr]; SetAttributes[f, {Protected, HoldFirst}] f = 2 Set::wrsym: Symbol f is Protected. >> ...


8

The main difference can been seen when dealing with list of lists. Consider the following list: lis = {{1, 2, 3}, {3, 4, 5}, {5, 6, 7}}; Lets create a Listable function g SetAttributes[g, Listable] Now we Map a non-listable function f and apply the listable function g Map[f, lis] (* {f[{1, 2, 3}], f[{3, 4, 5}], f[{5, 6, 7}]} *) g[lis] (* {{g[1], ...


4

The following is an expansion of the explanation given by Mr.Wizard. The pattern-matcher works on the base of the assumption that Orderless attribute is already applied and the arguments of the Orderless function are already sorted in the canonical order: ClearAll[o] SetAttributes[o, Orderless] MatchQ[Hold[o[y, x, a]], Hold[o[_, x, a]]] (* unsorted ...


4

UPDATE That question is by the essence an exact duplicate of this one. The explanation given by Mr.Wizard means that the pattern-matcher is NOT capable to handle situations when an unevaluated function with Orderless attribute is wrapped by Hold. So this is indeed a gedanken functionality. The pattern-matcher works on the base of the assumption that ...


3

You can put a Verbatim on the Plus: MatchQ[Hold[x + 2 y + 0], Hold[Verbatim[Plus][x, 2 _, 0]]] (* True *) Another way: expr = Inactivate[x + 2 y + 1]; form = Inactivate[x + 2 _ + 1]; MatchQ[expr, IgnoringInactive@form] (* True *)



Top 50 recent answers are included