17

I've been playing around with sequences a bit. In particular with using ## with unary and binary operators.

Let's start simple, the following all make some kind of sense:

  + ## & [a,b] (* a + b *)
x + ## & [a,b] (* x + a + b *)
x * ## & [a,b] (* x * a * b *)
x ^ ## & [a,b] (* x ^ a ^ b *)

Now here is a slightly weird case:

  - ## & [a,b] (* -a*b *)
x - ## & [a,b] (* x - a*b *)

I guess, this sort of makes sense if - is actually interpreted as something like +(-1)*. But it also means that +##-## is generally non-zero.

But now here's the real puzzle:

x / ## & [a,b]   (* x a^(1/b) *)
x / ## & [a,b,c] (* x a^b^(1/c) *)

Wh... what? Can anyone explain what's happening here or at least give some justification like the one for subtraction? Answers which correct my explanation for subtraction are also welcome!

(No, I would never use this stuff in production code. But knowing what exactly is going on under the hood could come in handy some time.)

Bonus Question: Are there any other operators that yield unexpected and potentially "useful" results? (I mean, !## will yield Not[a,b] but that's neither very unexpected nor useful.)

Martin Ender
  • 8,774
  • 1
  • 34
  • 60

2 Answers2

18

The documentation for Minus states that

-x is converted to Times[-1,x] on input.

So -Sequence[a,b] == Times[-1,Sequence[a,b]] == Times[-1,a,b] by this definition. Similarly the documentation for Divide states that

x/y is converted to x y^-1 on input.

and therefore x / Sequence[a,b] == x Sequence[a,b]^-1. Sequence[a,b]^c == Power[a, Power[b,c]]. When c == -1 you get Power[b, -1] == 1/b.

C. E.
  • 70,533
  • 6
  • 140
  • 264
17
x/## & // FullForm
Function[Times[x,Power[SlotSequence[1],-1]]]

and Power[a,b,c...] == Power[a, Power[b, c...]] so now it should be clear.

This syntax is mentioned in the last bullet point in details of Power documentation.

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • 4
    beat me by a femtosecond – Dr. belisarius Jan 08 '15 at 18:08
  • Ah, thanks a lot. I was thinking it would probably be analogous to the minus case, but couldn't work it out. I think linking to the documentation for Divide would be more useful than linking to the documentation for Power though. – Martin Ender Jan 08 '15 at 18:12