3
$\begingroup$

Throwaway comment in Hazrat p105: Observe that Range[10]/.{x_,y___}->y/x amounts to 10!

I can't for the life of me figure out how this works.

It seems to resolve to Sequence[2,3,4,5,6,7,8,9]/1 which then defaults to Times[2,3,4,5,6,7,8,9]/1 which seems like odd behaviour.

I checked the documentation but this doesn't resemble their examples of a sequence being ready to splice into another function.

Any comments?

$\endgroup$
  • $\begingroup$ Ponder on the following: Range[10] /. {x_, y___} -> FullForm[HoldComplete[y/x]]. In general, when in doubt as to how things are evaluated, look at its FullForm[]. $\endgroup$ – J. M. is away Apr 6 '17 at 3:36
  • $\begingroup$ This is because Sequence[2,3,4,5,6,7,8,9]/1 is really Times[Sequence[2,3,4,5,6,7,8,9], Power[1, -1]] behind the scenes, which is the same as Times[2,3,4,5,6,7,8,9, Power[1, -1]]. The point is that a/b // FullForm is Times[a, Power[b, -1]]. $\endgroup$ – march Apr 6 '17 at 3:38
  • $\begingroup$ Related, perhaps duplicate: (71348) $\endgroup$ – Mr.Wizard Apr 6 '17 at 4:44
  • $\begingroup$ Thanks all. That's subtle and I will try and add HoldComplete//FullForm to my toolkit. $\endgroup$ – Joe Apr 7 '17 at 2:13
2
$\begingroup$

I described form like this in answer to HoldForm[Operator ##] on some list. Using a method from Using a list of tuples in a pure function we can see what y/x actually means to Mathematica:

HoldForm[FullForm[ y/x ]]
Times[y, Power[x, -1]]

As discussed in Why are numeric division and subtraction not handled better in Mathematica? the / operator is silently converted into a combination of Times and Power, and it is this expression into which the sequence bound to y is inserted.

So we have directly:

Times[2, 3, 4, 5, 6, 7, 8, 9, 10, Power[1, -1]]

It is arguable whether or not Sequence is involved here. (I remembering arguing this with Leonid as a matter of fact.)

Note that Divide is not interpreted in the same manner:

Range[5] /. {x_, y__} :> Divide[x, y]

Divide::argrx: Divide called with 5 arguments; 2 arguments are expected. >>

Divide[1, 2, 3, 4, 5]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.