Re:Exotic Programming Ideas, Part 2: Term Rewriting
by tgb on Monday November 16, 2020 @11:37AM
(#25113674)
What's the magic going on in substitutions like:
which yields 'Cos[a+b]'. Is the underscore after the x what indicates that "x_" can take any value? And Mathematica then strips the undescore and replaces 'x' with that value? Why not just use x_ on both sides? And earlier the author was using '_x' instead of 'x_'. It seems rather mysterious to me.
[ Parent
| on HN ]
Re:Exotic Programming Ideas, Part 2: Term Rewriting
by kmill on Monday November 16, 2020 @01:42PM
(#25115353)
The left-hand side of the arrow is a pattern expression [1] (though usually you'd use the :> arrow to delay evaluation of the right-hand side). The "full" notation is x_H for a pattern variable x that needs to have head H. Dropping the H, it matches anything. Dropping the x, it just doesn't set a variable when matching. Dropping both, it matches anything and doesn't set a variable.
[1] https://reference.wolfram.com/language/guide/Patterns.html
Re:Exotic Programming Ideas, Part 2: Term Rewriting
by tgb on Monday November 16, 2020 @01:56PM
(#25115518)
Okay thanks, that all makes more sense now.
Re:Exotic Programming Ideas, Part 2: Term Rewriting
by 6gvONxR4sf7o on Monday November 16, 2020 @12:35PM
(#25114494)
It matches the expression with x_ as a named wildcard (it has lots of stuff you can do to constrain the matching, like “only match if x is prime”). Then it replaces the whole expression having x bound to whatever x_ matched.
Re:Exotic Programming Ideas, Part 2: Term Rewriting
by colehasson on Monday November 16, 2020 @11:45AM
(#25113801)
the underscore makes it a symbolic variable, not one with value
unbound in the lambda sense
Re:Exotic Programming Ideas, Part 2: Term Rewriting
by tgb on Monday November 16, 2020 @11:50AM
(#25113856)
So any underscore at all makes it a symbolic variable? (Or trailing/leading underscores only?) And the name of the variable on the right-hand side of the substitution is that name with the (or all?) underscore removed? It seems very clunky, so I think I'm missing something.
Re:Exotic Programming Ideas, Part 2: Term Rewriting
by Someone on Monday November 16, 2020 @12:32PM
(#25114449)
Mathematica doesn’t allow underscores in names. I also think the use of _x is an error in this article (disclaimer: I haven’t used mathematics in decades)
⇒ underscores must come at the end.
Re:Exotic Programming Ideas, Part 2: Term Rewriting
by kmill on Monday November 16, 2020 @01:45PM
(#25115385)
A pattern like _List matches lists, but the match isn't associated to a variable.
Re:Exotic Programming Ideas, Part 2: Term Rewriting
by Someone on Tuesday November 17, 2020 @07:45AM
(#25123089)
Of course. Thanks! It’s name_type, where both name and type are optional.
Re:Exotic Programming Ideas, Part 2: Term Rewriting
by tgb on Monday November 16, 2020 @01:20PM
(#25115083)
So is the right interpretation to think of "_" not as part of the variable name at all but rather as a unary operator (that acts on its left) which denotes a variable as unbound?
Re:Exotic Programming Ideas, Part 2: Term Rewriting
by jazzyjackson on Monday November 16, 2020 @05:31PM
(#25117915)
It's not really an operator, it's part of a pattern-matching syntax. "_" is called Blank and matches anything. Putting a name in front of it just makes it available by name -- like referring to capture groups in a regex.
So when you call a function on some argument or list of arguments, the interpreter looks up all the function definitions and finds one with a pattern that matches.
Double[x_] := 2 * x
Will work for Double[3] but not for Double[3, 2], the latter doesn't match any patterns, and the interpreter doesn't know what to do with it. You would have to define another function with the same name to handle other patterns, like overloading in C++You can further constrain the pattern to types like this:
Double[x_Integer] := 2 * x
And now the pattern only matches integers.
More in the manual, "Patterns"[0] and "More about Patterns"[1][0] https://www.wolfram.com/language/elementary-introduction/2nd...
[1] https://www.wolfram.com/language/elementary-introduction/2nd...
Re:Exotic Programming Ideas, Part 2: Term Rewriting
by 6gvONxR4sf7o on Monday November 16, 2020 @12:37PM
(#25114530)
At this point you seem too understand the mechanism well enough that the docs will help you with the details best.