# Trouble with expressing "anything" using patterns?

Suppose I have a list:

L = {p, p \[Implies] q}


I want to create a function that I can apply to L (f/@L) such that whenever x is in L and x \[Implies] anything, then append anything to L. I think this function would be something like:

f[x_] := If[MemberQ[L, (x \[Implies] _)], AppendTo[L, _], Nothing]


But I'm having trouble expressing the "anything".

Here are a couple of way, but be warned that we'll need to fix some ambiguities in your question and we'll need to determine whether you really need mutation side-effects:

L = {p, p \[Implies] q};

f1[x_] :=
With[
{matches = Cases[L, x \[Implies] _]},
Join[L, Last /@ matches]]

f1[p]
(* {p, p \[Implies] q, q} *)

f1[q]
(* {p, p \[Implies] q} *)


Now, if L were this:

L = {p \[Implies] q, p \[Implies] r}


then

f1[p]
(* {p \[Implies] q, p \[Implies] r, q, r} *)


but only if L had been defined thus prior to defining f1. If you don't want that constraint, then we need to add our list-that-might-contain-implications as an argument to f1. Also, this doesn't actually change L, it just returns a list that contains the elements of L and the new elements we want to add. If you need to actually change L then we'd need to add that bit of functionality.

Now, if you don't need the results appended, but just added somewhere in the returned list, you could do this:

f2[x_] := L /. (x \[Implies] y_) :> Splice[{x \[Implies] y, y}]

f2[p]
(* {p, p \[Implies] q, q} *)


or if we had a different L:

L = {p \[Implies] q, p \[Implies] r};
f2[x_] := L /. (x \[Implies] y_) :> Splice[{x \[Implies] y, y}];
f2[p]
(* {p \[Implies] q, q, p \[Implies] r, r} *)


Again, the sequence of evaluations matters, so the same questions from above need to be resolved.

To be clear, this doesn't actually answer your question exactly. I don't know how to resolve the questions and also don't know what aspects of your question are important. For example, you say you want to use the function like f/@L, but maybe that was just an idea you had and not a true requirement. Also, you said you wanted to know "how to express 'anything' using patterns", and I didn't really answer that directly or explicitly. So, if your question is really a deeper pattern-matching question that goes beyond this example, then we'd need to expand the discussion.

You can use ReplaceAll and named Pattern with RuleDelayed if you are not too concerned with the point of insertion in the list.

f[x_, lst_] := lst /. h : (x \[Implies] j : _) :> Sequence[h, j]


then

l1 = {p, p \[Implies] q};
f[p, l1]

{p, p \[Implies] q, q}


and

l2 = {p, p \[Implies] q, t \[Implies] c, p \[Implies] s, p \[Implies] s};
f[p, l2]

{p, p \[Implies] q, q, t \[Implies] c, p \[Implies] s, s, p \[Implies] s, s}


Hope this helps.