0
$\begingroup$

I am reading this page : https://reference.wolfram.com/language/tutorial/FunctionsWithVariableNumbersOfArguments.html

And I don't understand the example : h[a___, x_, b___, x_, c___] := hh[x] h[a, b, c]

At the exact first call, which vales take x,a,b,c on the call ? Because I am confused with thoose ___.

$\endgroup$
11
  • $\begingroup$ In the linked page, there are explanations of those blanks right below this example. $\endgroup$
    – vapor
    Jun 2, 2017 at 10:33
  • $\begingroup$ A brief explanation: the two x_ represents any two duplicated elements, and a, b, c with blank null sequence pattern can represent anything. Together h will pick out duplicated elements regardless of their positions. If your first call refers to h[2, 3, 2, 4, 5, 3], then a is Sequence[](Nothing), b is 3, c is Sequence[4,5,3] and x is 2, the expression becomes ` hh[2] h[3,4,5,3]. Then the latter part can also be matched, with a` is Sequence[], b is Sequence[4,5], c is Sequence[] and x is 3, then the final answer is h[4, 5] hh[2] hh[3] $\endgroup$
    – vapor
    Jun 2, 2017 at 10:41
  • $\begingroup$ @happyfish I think the explanation in the reference might not be so clear. Why not post your comment as an answer? This question should be OK, IMO. $\endgroup$
    – LLlAMnYP
    Jun 2, 2017 at 11:04
  • $\begingroup$ When you say h will pick out duplicated elements. Here we have $2$ and $3$ that have two occurences. How the function knows $x=2$ and not $x=3$ for example ? Is it the first duplicated elements that the functions meet ? Then as $2$ is the first duplicated, the program knows that $a$ has to be nothing ? $\endgroup$
    – StarBucK
    Jun 2, 2017 at 11:27
  • $\begingroup$ So you are basically asking how pattern matching is implemented in mathematica? I do not have an answer. But you can look up algorithms that are commonly used, e.g. Thompson's construction $\endgroup$
    – vapor
    Jun 2, 2017 at 13:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.