Question: using pattern matching in Maple to select sublists

This is an excersise in one of Mathematica's tutorials. The problem is to find from all persumtations of [1,2,3,4] those lists which has 2 in them before 3.

But the idea is that 2 can be anywhere before the 3, with possibly zero or more numbers between.

Will show how to do this in that other software, and ask if there is a way using Maple pattern matching or better way to do this in Maple better than what I did.

L=Permutations[{1,2,3,4}]
Cases[L,{___,2,___,3,___}]

gives

{{1, 2, 3, 4}, {1, 2, 4, 3}, {1, 4, 2, 3}, {2, 1, 3, 4}, 
{2, 1, 4,  3}, {2, 3, 1, 4}, {2, 3, 4, 1}, {2, 4, 1, 3}, 
{2, 4, 3, 1}, {4, 1,  2, 3}, {4, 2, 1, 3}, {4, 2, 3, 1}}

We see in all of the above, 2 is before 3.

In that other software, the ___ means there is zero or more things.  I could not find how to do this in Maple's pattern matching. So had to use has and then find the index of 2 and 3 in each list and check if the index of 2 is smaller than the index of 3. Which is kinda awakard and not as elegent as using a pattern, but it gives same result.

L:=combinat:-permute([1,2,3,4]);
map(X->`if`(has(X,2) and has(X,3) and ListTools:-Search(2,X)<ListTools:-Search(3,X),X,NULL) ,L);

Gives

[[1, 2, 3, 4], [1, 2, 4, 3], [1, 4, 2, 3], [2, 1, 3, 4], 
[2, 1, 4, 3], [2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 1, 3], 
[2, 4, 3, 1], [4, 1, 2, 3], [4, 2, 1, 3], [4, 2, 3, 1]]

Can you suggest a way using either patmatch or applyrules in Maple to do the same?  

Could this be done easier than what I did using evalindents without the need for pattern?

I find patterns easier to work with myself.

ps. converting each list to string, and then using regular expression or string matching, is not what I am looking for. 

ps. the check in Maple code of has(X,2) and has(X,3) is not really needed here, since we know each list will have 2 and 3 in them. But I kept these to make it more general for other type of problems where these extra checks might be needed.

Please Wait...