Questions tagged [pattern-matching]

Use this tag for questions about testing whether a data structure has a particular shape or contains particular values in certain locations. Many functional languages provide pattern matching constructs. Most questions in this tag should also have the tag for the language you are programming in. DO NOT USE THIS TAG FOR REGULAR EXPRESSION QUESTIONS, USE [regex] INSTEAD; similarly, for pattern matching (globbing) in POSIX-like shells, use [glob].

What is pattern matching?

Pattern matching means checking whether a data structure conforms to a certain pattern. In the abstract, a pattern can be any set of values; however most languages restrict patterns to expressing structural constraints, such as “all lists with at least two elements” or “all 2x2 matrices where the elements at (1,0) and (0,1) are equal”.

Languages such as ML, Haskell, Erlang and Mathematica have core language constructs for pattern matching. Other languages such as Lisp have derived pattern matching constructs.

Pattern-matching enhances a language in two directions: expressiveness, as complex sequences of tests can be written concisely, and performance, as the compiler is able (at least when pattern-matching is a core language construct) to optimize such constructs to generate the optimal number of tests (i.e. never checking twice the same condition).

Tag usage guidance

Regular expressions are an important special case of pattern matching on strings. Do not use the tag for regular expression questions, use the tag instead.

Pattern matching is normally exact. If you're looking for approximate patterns, for example in image or speech analysis, look for “recognition” rather than “matching”, for example .

8954 questions
4
votes
1 answer

Can I use regexes containing ampersands in HTML5 pattern attributes?

I use the following email validation before submission of a form in an HTML5 webapp:
Per Quested Aronsson
  • 11,380
  • 8
  • 54
  • 76
4
votes
1 answer

Quotations and pattern matching in F#

In a new console application, just pasting the following code leads to the exception "The parameter is not a recognized method name". Does the following code work on your installation? Joker: Do you know any reason why it would not work on…
nicolas
  • 9,549
  • 3
  • 39
  • 83
4
votes
1 answer

searching on a large database

I have a very large 3D matrix and I need to call some specific patterns with a special configuration from this large matrix. For example, I need a sub-matrixes which their a,b,c,..,h element are equal with a specific value (it is not pattern…
Sam
  • 939
  • 5
  • 14
  • 41
4
votes
2 answers

How to avoid syntax overhead for def definition with pattern matching in Scala?

How to avoid to wrap args when implementing a def with pattern matching ? Examples : def myDef(a: A, b:B, c: C): D = (a,c,d) match { case ('qsdqsd, _ , _ ) => ??? case _ => ??? }
jwinandy
  • 1,739
  • 12
  • 22
4
votes
2 answers

F# overly aggressive type inference?

So in doing some of the Project Euler problems, I want to be able to take the square root of integer values (int, long, bigint, etc), but Sqrt is only defined for floating-point values. So I've been writing my own little Newton-Raphson algorithm,…
Lee Crabtree
  • 1,196
  • 2
  • 12
  • 30
4
votes
7 answers

Algorithm for linear pattern matching?

I have a linear list of zeros and ones and I need to match multiple simple patterns and find the first occurrence. For example, I might need to find 0001101101, 01010100100, OR 10100100010 within a list of length 8 million. I only need to find the…
erjiang
  • 44,417
  • 10
  • 64
  • 100
4
votes
2 answers

Implicit parameters won't work on unapply. How to hide ubiquitous parameters from extractors?

Apparently unapply/unapplySeq in extractor objects do not support implicit parameters. Assuming here an interesting parameter a, and a disturbingly ubiquitous parameter b that would be nice to hide away, when extracting c. [EDIT]: It appears…
ib84
  • 675
  • 5
  • 16
4
votes
1 answer

Extracting all occurrences of repeated and unique patterns from text, along with context

Say I have the text "abcabx". I would like to know that there is a repeated pattern "ab", all the locations it appears, and how the context of those repetitions relates to its other occurrences. I also want the data structure to have the unique…
user173342
  • 1,820
  • 1
  • 19
  • 45
4
votes
3 answers

Lua: How to check if a string contains only numbers and letters?

Simple question may have a simple answer, but my current solution seems horrible. local list = {'?', '!', '@', ... etc) for i=1, #list do if string.match(string, strf("%%%s+", list[i])) then -- string contains characters that are not…
unwise guy
  • 1,048
  • 8
  • 18
  • 27
4
votes
1 answer

Threading `Try`s through for-comprehension

Triggered by another question (which has been subsequently edited away though), I wanted to try out how easy it would be to chain calls to Scala 2.10's Try construct (cf. this presentation), using for-comprehensions. The idea is to have a list of…
0__
  • 66,707
  • 21
  • 171
  • 266
4
votes
2 answers

Recognize Black patterns appearing on the four corners of the image ios using opencv or some other technique

I am stuck with a problem that is how to recognize some patterns in image. the image is the image of paper which is pure white and the patterns are in four corners are in black. I want to recognize the black patterns on the image? I surf a lot on…
Gypsa
  • 11,230
  • 6
  • 44
  • 82
4
votes
2 answers

Add specific value to a data.frame column by matching a pattern

I have two data.frames: pattern <- data.frame(pattern = c("A", "B", "C", "D"), val = c(1, 1, 2, 2)) match <- data.frame(match = c("A", "C")) I want to add to my data.frame pattern another column called new_val and assign "X" to each row where the…
user969113
  • 2,349
  • 10
  • 44
  • 51
4
votes
5 answers

Python finding patterns within large group of numbers?

I'm working with a list of lists that have the periods of continued fractions for non-perfect square roots in each of them. What I'm trying to do with them is to check the size of the largest repeating pattern in each list. Some of the lists for…
tijko
  • 7,599
  • 11
  • 44
  • 64
4
votes
2 answers

How can detect an image within another image in Java?

Hi I have a color palette like this one, I have an image. the image contains a color palette like this. I need to detect this color palett from the image and crop the color palette from the image. Are their any libraries in java to do that. That…
P basak
  • 4,874
  • 11
  • 40
  • 63
4
votes
3 answers

Matching patterns between multiple columns

I have two columns say Main and Sub. (they can be of same table or not). Main is varchar of length 20 and Sub is varchar of length 8. Sub is always subset of Main and it is last 8 characters of Main. I could successfully design a query to match…
Anuj Patel
  • 17,261
  • 3
  • 30
  • 57
1 2 3
99
100