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$
4
  • $\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$ Commented Apr 6, 2017 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$ Commented Apr 6, 2017 at 3:38
  • $\begingroup$ Related, perhaps duplicate: (71348) $\endgroup$ Commented Apr 6, 2017 at 4:44
  • $\begingroup$ Thanks all. That's subtle and I will try and add HoldComplete//FullForm to my toolkit. $\endgroup$ Commented Apr 7, 2017 at 2:13

1 Answer 1

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$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.