# What is the pattern for an argument that must be a List of Integers

**URL:** <https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763>\
**Category:** Questions\
**Created:** [March 16, 2022, 9:34pm UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763 "2022-03-16T21:34:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![henrickjeanty](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrickjeanty/32/361_2.png) [@henrickjeanty](https://community.wolfram.com/u/henrickjeanty)\
**Post date:** [March 16, 2022, 9:34pm UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/1 "2022-03-16T21:34:59Z")

</div>

Given a function f defined as f[x\_] := …  
I understand that x\_ can be anything.  
If I want the argument x to be an Int, I would write f[x\_Integer] := …  
If I want the argument x to be a List, I would write f[x\_List] := …  
My question is now how would I specify that the argument x MUST BE a list of Ints so that I can call f like this f[{1,2,3}]

---

<div class="post-metadata">

**Author:** ![sghuisman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sghuisman/32/27850_2.png) [@sghuisman](https://community.wolfram.com/u/sghuisman)\
**Post date:** [March 16, 2022, 9:39pm UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/2 "2022-03-16T21:39:04Z")

</div>

F[x:{(\_Integer)…}] := x ?

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [March 16, 2022, 10:14pm UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/3 "2022-03-16T22:14:57Z")

</div>

f[ns : {\_\_\_Integer}] = whatever (_this includes an empty list_)  
f[ns : {\_\_Integer}] = whatever (_this excludes an empty list_)

---

<div class="post-metadata">

**Author:** ![henrickjeanty](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrickjeanty/32/361_2.png) [@henrickjeanty](https://community.wolfram.com/u/henrickjeanty)\
**Post date:** [March 16, 2022, 10:30pm UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/4 "2022-03-16T22:30:02Z")

</div>

That works for a list of Integers. Thank you!  
But how about an argument that could be a List of List of Integers as in calling F[{{1,2,3},{4,5,6}}] ?

---

<div class="post-metadata">

**Author:** ![sghuisman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sghuisman/32/27850_2.png) [@sghuisman](https://community.wolfram.com/u/sghuisman)\
**Post date:** [March 16, 2022, 10:34pm UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/5 "2022-03-16T22:34:44Z")

</div>

You would need to do something like:

```
F[x_?(ArrayQ[#,1|2,IntegerQ])] :=

```

Look at pattern testing and ArrayQ

---

<div class="post-metadata">

**Author:** ![henrickjeanty](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrickjeanty/32/361_2.png) [@henrickjeanty](https://community.wolfram.com/u/henrickjeanty)\
**Post date:** [March 16, 2022, 10:59pm UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/6 "2022-03-16T22:59:41Z")

</div>

I will do that though I have read a lot about patterns.  
In any case, something that would be great would be a repository of patterns. Some already exist in the documentation, but we would need more.  
I know that with regular expressions there are dictionaries of patterns for things like:  
valid credit card #'s  
phone #'s  
etc..  
What would be nice would be able to give a pattern a name like  
ListOfIntegers = {\_ \_ _Integer}  
then one could compose larger patterns as in  
ListOfListOfIntegers = {_ \_ \_ListOfIntegers } meaning a ListOfListOfIntegers is made up of a list of 1 or more ListsOfIntegers

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [March 17, 2022, 12:07am UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/7 "2022-03-17T00:07:17Z")

</div>

> What would be nice would be able to give a pattern a name…

You can certainly do this.

```
MyPattern = {_String, {__Integer}, _Symbol}

TheSymbols[structs : {MyPattern ...}] := structs[[All, -1]]

TheSymbols[{{"a", RandomInteger[10, 2], abc}, {"b", RandomInteger[10, 2], xyz}}]
(*returns {abc, xyz}*)

```

---

<div class="post-metadata">

**Author:** ![henrickjeanty](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrickjeanty/32/361_2.png) [@henrickjeanty](https://community.wolfram.com/u/henrickjeanty)\
**Post date:** [March 17, 2022, 3:31am UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/8 "2022-03-17T03:31:06Z")

</div>

Never thought of that! Thank you!  
So, is there some sort of repository of pattern expressions for commonly used patterns?  
Things like patterns for URLs, IP addresses, street addresses, etc…?

---

<div class="post-metadata">

**Author:** ![mroge02](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mroge02/32/28775_2.png) [@mroge02](https://community.wolfram.com/u/mroge02)\
**Post date:** [April 2, 2022, 3:10am UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/9 "2022-04-02T03:10:25Z")

</div>

Here’s a recursive pattern test that checks for lists of lists of…of lists of integers, that is, of arbitrarily nested lists of integers.

```
nestedListOfIntegerQ = MatchQ[#, {(_Integer | _?nestedListOfIntegerQ) ...}] &;

f[x_?nestedListOfIntegerQ] := Depth[x];

```

More examples of nested lists:

```
MatchQ[{1, 2, 3}, _?nestedListOfIntegerQ]
(* True *)

MatchQ[{{1, 2, 3}, {4, 5, 6}}, _?nestedListOfIntegerQ]
(* True *)

MatchQ[{{2}, {3, {4}, 5}}, _?nestedListOfIntegerQ]
(* True *)

(* Allows empty lists *)
MatchQ[{{}, {{}}}, _?nestedListOfIntegerQ]
(* True *)

```

Use double-dot `..` (`Repeated`) instead of the triple-dot `...` (`RepeatedNull`) to match nested lists, none of which are empty. If you want a mix, you’ll have to specify an explicit pattern for the specific mix.

---

<div class="post-metadata">

**Author:** ![henrickjeanty](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrickjeanty/32/361_2.png) [@henrickjeanty](https://community.wolfram.com/u/henrickjeanty)\
**Post date:** [April 5, 2022, 1:46pm UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/10 "2022-04-05T13:46:40Z")

</div>

Hello Michael,  
Thank you very much. I will go and play with what you have taught me.

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [April 5, 2022, 4:59pm UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/11 "2022-04-05T16:59:15Z")

</div>

> So, is there some sort of repository of pattern expressions for  
> commonly used patterns? Things like patterns for URLs, IP addresses,  
> street addresses, etc…?

Not patterns but [`Interpreter`](http://reference.wolfram.com/language/ref/Interpreter.html) can do that.

---

<div class="post-metadata">

**Author:** ![wernergg](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wernergg/32/6481_2.png) [@wernergg](https://community.wolfram.com/u/wernergg)\
**Post date:** [April 6, 2022, 5:22pm UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/12 "2022-04-06T17:22:42Z")

</div>

I do not understand the Repeated patterns (“..” and “…”). Let me explain:

You can pick particular elements matching some conditions out of a list by Cases:

\text{Cases}[\{1,2.,3+i\},\text{e$\_$}\text{/;}e\in \mathbb{R}\land e\>1]

==\>  
{2.}

If you Trace that you can see that Cases applies the conditions on each list element:

\text{Trace}[\text{Cases}[\{1,2.,3+i\},\text{e$\_$}\text{/;}e\in \mathbb{R}\land e\>1]]

==\> …

But I don’ t know how to match a list with such properties:

\text{MatchQ}[\{1,2.,3+i\},\{(\text{e$\_$}\text{/;}e\in \mathbb{R}\land e\>1)\text{...}\}]

==\>  
False

… looks nice, but indeed this matches no list at all:

\text{MatchQ}[\{1.1,2.,3+1\},\{(\text{e$\_$}\text{/;}e\in \mathbb{R}\land e\>1)\text{...}\}]

==\>  
False

If you Trace that you can see that MatchQ stops checking after the first list element and returns False in any case:

Hence I cannot define a function that accepts a list of real integers greater than 1.

\text{Block}[\{f\},f(\text{xs}:\{(\text{e$\_$}\text{/;}e\in \mathbb{R}\land e\>1)\text{...}\})\text{:=}\text{xs}-1;f(\{1.1,2.,3+1\})]

==\>  
f[{1.1,2.,4}]

_Note that “{(\_)…}” is a correct pattern for any list:_

\text{MatchQ}[\{1.1,2.,3+1\},\{(\_)\text{...}\}]

==\>  
True

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [April 6, 2022, 7:09pm UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/13 "2022-04-06T19:09:40Z")

</div>

Hi Werner,

> Hence I cannot define a function that accepts a list of real integers  
> greater than 1.

How about this?

```
Block[{f},
 f[xs : {a : (_Real | _Integer) ..} /; AllTrue[{a}, # > 1 &]] := xs;
 Print[f[{2., 2, 3 + I}]];
 Print[f[{1., 1}]];
 Print[f[{.5, 2, 3 + 1}]];
 Print[f[{2., 2, 5/2}]];
 Print[f[{2., 1., 3 + 1}]];
 Print[f[{2., 2, 3 + 1}]];
 ]
(*
f[{2.,2,3+I}]
f[{1.,1}]
f[{0.5,2,4}]
f[{2.,2,5/2}]
f[{2.,1.,4}]
{2.,2,4}
*)

```

---

<div class="post-metadata">

**Author:** ![mroge02](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mroge02/32/28775_2.png) [@mroge02](https://community.wolfram.com/u/mroge02)\
**Post date:** [April 6, 2022, 11:25pm UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/14 "2022-04-06T23:25:31Z")

</div>

If you name the pattern, then all matches have to be the same expression:

```
MatchQ[{1.1, 2., 3 + 1}, {(e_ /; e ∈ Reals \[And] e > 1) ...}]
(* False : named, all three numbers are different *)

MatchQ[{2., 2., 2.}, {(e_ /; e ∈ Reals \[And] e > 1) ...}]
(* True : names, all are the Real 2. *)

MatchQ[{2., 2., 2}, {(e_ /; e ∈ Reals \[And] e > 1) ...}]
(* False : named, mix of Real 2. and Integer 2 -- not the same, even if equal *)

MatchQ[{2, 2, 2}, {(e_ /; e ∈ Reals \[And] e > 1) ...}]
(* True : named, all are the Integer 2 *)

MatchQ[{1.1, 2., 3 + 1}, {_?(# ∈ Reals \[And] # > 1 &) ...}]
(* True : unnamed pattern *)

```

The last with a `PatternTest` instead of a `Condition` is how to create the pattern with `RepeatedNull`.

---

<div class="post-metadata">

**Author:** ![wernergg](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wernergg/32/6481_2.png) [@wernergg](https://community.wolfram.com/u/wernergg)\
**Post date:** [April 7, 2022, 8:18am UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/15 "2022-04-07T08:18:10Z")

</div>

Thanks, Rohit. Of course there many way to achieve my goal. I wanted to understand why I cannot do it with the simple condition:

```
f[xs:{(e_/;e∈ℝ&&e>1)...}]

```

Above Michael Rogers explained that I have to use (unnamed) PatternTest.

BTW: Your definition with “\_Real | \_Integer” would miss rationals, hence had to use

```
_Real | _Integer | _Rational

```

---

<div class="post-metadata">

**Author:** ![wernergg](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wernergg/32/6481_2.png) [@wernergg](https://community.wolfram.com/u/wernergg)\
**Post date:** [April 7, 2022, 8:23am UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/16 "2022-04-07T08:23:21Z")

</div>

Great, Michael. I read that restriction “… same expression …”, but did not understand it. Now I learned why and how to use PatternTest.

---

<div class="post-metadata">

**Author:** ![wernergg](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wernergg/32/6481_2.png) [@wernergg](https://community.wolfram.com/u/wernergg)\
**Post date:** [April 7, 2022, 1:15pm UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/17 "2022-04-07T13:15:34Z")

</div>

Michael, just another question, concerning PatternTest for testing more than one variable.

As an example I want to match a list of 2D-vectors where y is greater than x. I can write:

```
MatchQ[{{1, 2}, {Pi, 3.2}}, {_?(#[[1]] < #[[2]] &) ...}]

```

==\> True

But I cannot use a more readable form like:

```
MatchQ[{{1, 2}, {Pi, 3.2}}, {{_, _}?(#1 < #2 &) ...}]

```

==\>

![4313b.png](https://global.discourse-cdn.com/wolfram/original/2X/9/9a2f7d384285d5dbbf210d83c48e055a4edab417.png)

Do you know if there is something the like?

---

<div class="post-metadata">

**Author:** ![mroge02](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mroge02/32/28775_2.png) [@mroge02](https://community.wolfram.com/u/mroge02)\
**Post date:** [April 7, 2022, 4:31pm UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/18 "2022-04-07T16:31:28Z")

</div>

Hi Werner,

The ways I can think of immediately are three or four. Since `PatternTest` takes as an argument only the whole expression to be tested, if you want to test a relationship between its parts, then you have to get at the parts in some way.

Your first way, with `Part`, seems clear except for the mass of punctuation symbols. The `[[]]` can be replaced by words, `First` and `Last`. Since the length of `#` is 2, `Last[#]` gives the same result as `#[[2]]`. This might be appealing to some people:

```
MatchQ[{{1, 2}, {Pi, 3.2}}, {{_, _}?(#[[1]] < #[[2]] &) ...}]
(*True *)

MatchQ[{{1, 2}, {Pi, 3.2}}, {{_, _}?(First[#] < Last[#] &) ...}]
(*True *)

```

Here is a `Less` readable version (hope you don’t mind a pun):

```
MatchQ[{{1, 2}, {Pi, 3.2}}, {{_, _}?(Less @@ # &) ...}]
(* True *)

```

I hope the following is not confusing. The named patterns in `{x_, y_}` are a separate from the nameless pattern `{_, _}` being tested and repeated:

```
MatchQ[{{1, 2}, {Pi, 3.2}}, {{_, _}?(MatchQ[#, {x_, y_} /; x < y] &) ...}]
(* True *)

```

This last way is more verbose and probably a bit less efficient. Maybe it’s more readable. If so, then unless you are apply such patterns to large amounts of data, the efficiency is probably not as significant a concern as readability. Frankly, it’s took me years and years to get comfortable with patterns. As you learn more, what seems readable changes, too.

Anyway, these are the ideas I could come up with. Pick whichever one you find easiest to deal with.

---

<div class="post-metadata">

**Author:** ![mroge02](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mroge02/32/28775_2.png) [@mroge02](https://community.wolfram.com/u/mroge02)\
**Post date:** [April 7, 2022, 4:36pm UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/19 "2022-04-07T16:36:59Z")

</div>

Even though your question focused on `RepeatedNull[]`, I maybe should have mentioned that

```
MatchQ[{1.1, 2., 3 + 1}, {_?(# ∈ Reals \[And] # > 1 &) ...}]

```

can also be done with `___` (`BlankNullSequence[]`) instead of `...`:

```
MatchQ[{1.1, 2., 3 + 1}, {___?(# ∈ Reals \[And] # > 1 &)}]

```

---

<div class="post-metadata">

**Author:** ![hansmilton](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hansmilton/32/8656_2.png) [@hansmilton](https://community.wolfram.com/u/hansmilton)\
**Post date:** [April 7, 2022, 5:31pm UTC](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763/20 "2022-04-07T17:31:25Z")

</div>

Not pattern matching, but works :

```
In[1]:= And @@ Map[#[[1]] < #[[2]] &, {{1, 2}, {Pi, 3.2}}]
Out[1]= True

In[2]:= And @@ Map[#[[1]] < #[[2]] &, {{1, 2}, {4, 3.2}}]
Out[2]= False

```

[Next page](https://community.wolfram.com/t/what-is-the-pattern-for-an-argument-that-must-be-a-list-of-integers/19763.md?page=2)
