3

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?

vapor
  • 7,911
  • 2
  • 22
  • 55
Joe
  • 1,467
  • 7
  • 13
  • 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[]. – J. M.'s missing motivation Apr 06 '17 at 03:36
  • 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]]. – march Apr 06 '17 at 03:38
  • Related, perhaps duplicate: (71348) – Mr.Wizard Apr 06 '17 at 04:44
  • Thanks all. That's subtle and I will try and add HoldComplete//FullForm to my toolkit. – Joe Apr 07 '17 at 02:13

1 Answers1

2

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]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371