# Why does this RuleDelayed behave weird?

This is something that I couldn't explain.

functions = {ConditionalExpression[Abs[1 - 2 x], x >= 1 || x <= 0],
ConditionalExpression[1 + x, x >= 0]};


My first function has two separate branches.

Plot[ConditionalExpression[Abs[1 - 2 x], x >= 1 || x <= 0], {x, -5,
5}, GridLines -> Automatic]


Now assume that I want to combine all lines into one Line head like this and plot it with ListLinePlot.
As you can see in the image, it has only one branch.

ListLinePlot @@
Cases[Plot[functions[[1]], {x, -5, 5}][[1]], {_Directive, l__Line} :>
If[Length@{l} == 1, l, Line[{l}[[All, 1]]]], All][[1]]


However, if I modify a bit and use {l} in the If function (not correct though) then it works for the first function and the image shows two branches.

ListLinePlot @@
Cases[Plot[functions[[1]], {x, -5, 5}][[1]], {_Directive, l__Line} :>
If[Length@{l} == 1, {l}, Line[{l}[[All, 1]]]], All][[1]]


Why does it behave like this?
Did I miss something or a bug? I'm using version 12.2.0.0 (windows)

A simpler example that replicates what you are seeing:

input1 = {Directive[blah], Line[foo], Line[bar]}

Cases[input1,
{_Directive, l__Line} :> If[Length@{l} == 1, l, Line[{l}[[All, 1]]]], All][[1]]

Line[bar]

Cases[input1,
{_Directive, l__Line} :> If[Length@{l} == 1, {l}, Line[{l}[[All, 1]]]], All][[1]]

Line[{foo, bar}]


The reason we are getting Line[bar] in the first case above is: When we use l in the second argument of If we are actually using a four-argument form of If where

If[condition,t,f,u] gives u if condition evaluates to neither True nor False.

Wrap If[...] with HoldForm to see what expression is processed on the rhs of RuleDelayed in the first case above:

Cases[input1, {_Directive, l__Line} :>
HoldForm[If[Length@{l} == 1, l, Line[{l}[[All, 1]]]]], All]


As an other illustration try a modified input that triggers an error message:

input2 = {Directive[blah], Line[foo], Line[bar], Line[buz]};

Cases[input2,
{_Directive, l__Line} :> If[Length@{l} == 1, l, Line[{l}[[All, 1]]]], All][[1]]


• Thanks, how can I make If behave as 3-arguments? How about HoldForm[l]? It works here but I'm not sure if that is the way to go. Aslo just curious why does it look like a dot in your Out[] instead of a number?
– hana
Commented Mar 24, 2022 at 18:06
• You can use If[Length@{l} == 1,First@{l}, Line[{l}[[All, 1]]]] instead of If[Length@{l} == 1, l, Line[{l}[[All, 1]]]]. Re Out[ ], it happens when I use Edit >> Copy As >> Bitmap ( don't know why.)
– kglr
Commented Mar 24, 2022 at 18:20
• Nice, I have never used copy Bitmap like that.
– hana
Commented Mar 24, 2022 at 18:26