Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6352989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:19:52+00:00 2026-05-24T22:19:52+00:00

While working on my answer to this question , it occurred to me that

  • 0

While working on my answer to this question, it occurred to me that it is difficult to match a finite range of elements. With the built in patterns, you can match 1 element (_), 1 or more elements (__), or zero or more elements (___). To match more than one element, I used PatternSequence, like this

a:PatternSequence[_,_,_]

or, more generically

a:PatternSequence@@Array[_&,3].

(Using a Condition would have also worked.) To match a range of n to m elements we could do

a:Alternatives@@( PatternSequence @@@ Array[_&, {n,m}] ),

but that is a rather convoluted way to accomplish something that can be done by

a__ /; n <= Length[{a}] <= m.

However, this brings up an interesting question, using the Condition form it is straightforward to match the range 0 to n,

a___ /; Length[{a}] <= n,

but can this be done using patterns alone, i.e. without using Condition (/;)? More specifically, how would one go about matching 0 elements without adding a condition? Also, which is faster?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:19:52+00:00Added an answer on May 24, 2026 at 10:19 pm

    Maybe you could do something with Repeated. E.g.

    Cases[{{1, 2, 3}, {1}, {1, 2, 3, 4, 5}, {1,2}}, {Repeated[_, {2, 4}]}]
    

    gives the same result as

    Cases[{{1, 2, 3}, {1}, {1, 2, 3, 4, 5}, {1,2}}, {a___ /; 2 <= Length[{a}] <= 4}]
    

    The first method seems faster than the second. For example

    tab = Table[Range[RandomInteger[1000]], {1000}];
    Timing[t1 = Cases[tab, {a___ /; 0 <= Length[{a}] <= 100}];]
    Timing[t2 = Cases[tab, {Repeated[_, {0, 100}]}];]
    SameQ[t1, t2]
    

    returns on my system

    {0.027801, Null}
    
    {0.000733, Null}
    
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While working on a question/answer program for school, it occurred to me that I
While working on an answer to this question I created this jsfiddle . For
I have looked everywhere for the answer to this question and while there are
i've been working with this for a while and i can't seem to figure
This is kind of a 'double' question that might have a single answer. I'm
I've asked this question before with no real answer. Can anybody help? I'm profiling
I was pretty sure that the answer to that question was, Never, ever can
I've been Googling for a while and can't seem to find an answer to
This question is regarding the behavior I observed while using const_cast for making a
I have this question you probably know the answer... My app is already functional

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.