The use of SlotSequence in If[#1 > #2, ##] &
The answer is simple: First, you really need to look up the documentation of ##
. It means "take all arguments". But more importantly, you can use the property that a > b
stays unevaluated when neither true or false can be computed. So check this out:
If[#1 > #2, ##] &[a, b]
(* If[a > b, a, b] *)
Do you see how ##
converts this construct into a simple "if a>b then a else b"?
Furthermore, Sequence[a,b,c]
is Mathematica's way of saying "a sequence of arguments that can be fed into a function". As you might know, it is not possible to evaluate the input a,b,c
without error. For normal functions, which don't have the attribute HoldAllComplete
, a Sequence[...]
expression is always flattened out.
func[Sequence[a, b, c]]
(* func[a, b, c] *)
This is exactly what happened in your If
. When you understand that
## &[22, 21]
(* Sequence[22, 21] *)
and you know it's turned into a real sequence (without the Sequence
around it) when inside a function, you understand what happened in your case.
To understand how the SlotSequence
(##
) works, use Trace
Trace[If[#1 > #2, ##] &[22, 21]]
Trace[If[#1 > #2, ##] &[21, 22]]