3 ms·
the underscore makes it a symbolic variable, not one with value unbound in the lambda sense
by colehasson 6y ago
the underscore makes it a symbolic variable, not one with value
unbound in the lambda sense
- tgb 6y agoSo 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.
- Someone 6y agoMathematica 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.
- tgb 6y agoSo 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?
- jazzyjackson 6y agoIt'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-ed/32-patterns.html https://www.wolfram.com/language/elementary-introduction/2nd... [1] https://www.wolfram.com/language/elementary-introduction/2nd-ed/41-more-about-patterns.html https://www.wolfram.com/language/elementary-introduction/2nd...
- kmill 6y agoA pattern like _List matches lists, but the match isn't associated to a variable.
- Someone 6y agoOf course. Thanks! It’s name_type, where both name and type are optional.
- 6gvONxR4sf7o 6y agoAt this point you seem too understand the mechanism well enough that the docs will help you with the details best.