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".


2 Answers 2


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_] :=
    {matches = Cases[L, x \[Implies] _]},
    Join[L, Last /@ matches]]

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

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

Now, if L were this:

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


(* {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}]

(* {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}];
(* {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]


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


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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