3
$\begingroup$

There are other similar questions on this site about using the Infix operator for more than two arguments, which I have read. But there is one thing that I wasn't able to understand and couldn't figure out why. Consider this line:

#~If~Sequence[2, 3] & /@ {False, True, False}

which gives the output {3, 2, 3} as expected. Now if I understand correctly, Sequence @@ {2, 3} is the same as Sequence[2, 3], but if you put this in the above line:

#~If~(Sequence @@ {2, 3}) & /@ {False, True, False}

it results in {Null, 2, 3, Null}. Why does this happen?

Is there some kind of workaround to make it work? I tried playing around with Hold and its variations without any luck.

$\endgroup$

1 Answer 1

9
$\begingroup$

Look at:

Attributes[If]
(* {HoldRest, Protected} *)

Now consider:

If[#1, Sequence @@ {2, 3}] &

Because of the "HoldRest", the Sequence will not be evaluated as you can see by e.g.:

If[#1, Sequence @@ {2, 3}] &[True]
(* Sequence[2, 3] *)

And the following returns Null:

If[#1, Sequence @@ {2, 3}] &[False]
$\endgroup$
1
  • 2
    $\begingroup$ Thanks. Therefore #~If~Evaluate[Sequence @@ {2, 3}] & /@ {False, True, False} gives the desired result $\endgroup$
    – polfosol
    Dec 9, 2021 at 12:53

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