Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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?


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...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: