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
57
votes
5 answers

How to use SIFT algorithm to compute how similar two images are?

I have used the SIFT implementation of Andrea Vedaldi, to calculate the sift descriptors of two similar images (the second image is actually a zoomed in picture of the same object from a different angle). Now I am not able to figure out how to…
Lazer
  • 90,700
  • 113
  • 281
  • 364
54
votes
2 answers

OCaml: Match expression inside another one?

I'm currently working on a small project with OCaml; a simple mathematical expression simplifier. I'm supposed to find certain patterns inside an expression, and simplify them so the number of parenthesis inside the expression decreases. So far I've…
Sergio Morales
  • 2,600
  • 6
  • 32
  • 40
53
votes
4 answers

How can I pattern match against an Option?

I can straight-forwardly match a String in Rust: let a = "hello".to_string(); match &a[..] { "hello" => { println!("Matches hello"); } _ => panic!(), } If I have an option type, it fails: match Some(a) { Some("hello") => { …
Phil Lord
  • 2,917
  • 1
  • 20
  • 31
52
votes
5 answers

Can a range be matched in Scala?

Is it possible to match a range of values in Scala? For example: val t = 5 val m = t match { 0 until 10 => true _ => false } m would be true if t was between 0 and 10, but false otherwise. This little bit doesn't work of course, but is…
Justin Poliey
  • 16,289
  • 7
  • 37
  • 48
51
votes
7 answers

Display only the n'th match of grep

onefish onechicken twofish twochicken twocows threechicken What if I want to grep for lines containing "two", but I only want the 2nd match. So I want the result "twochicken".
Andrew Tsay
  • 1,893
  • 6
  • 23
  • 35
50
votes
3 answers

Does structured binding work with std::vector?

Is it possible to use structured binding with vectors? E.g. std::vector vec{1, 2, 3}; auto [a, b, c] = vec; Above code unfortunately doesn't work (under GCC), but maybe there's a different way (with structured binding) that allows to assign…
BartekPL
  • 2,290
  • 1
  • 17
  • 34
50
votes
1 answer

Haskell record pattern matching

I'm looking for a way to simplify function patterns when the actual data is not required: data X = A | B String | C Int Int String myfn :: X -> Int myfn A = 50 myfn (B _) = 200 myfn (C _ _ _) = 500 Is there a way to make a simpler pattern for…
theduke
  • 3,027
  • 4
  • 29
  • 28
49
votes
2 answers

How to use switch/case (simple pattern matching) in Scala?

I've found myself stuck on a very trivial thing :-] I've got an enum: object Eny extends Enumeration { type Eny = Value val FOO, BAR, WOOZLE, DOOZLE = Value } In a code I have to convert it conditionally to a number (varianr-number…
Ivan
  • 63,011
  • 101
  • 250
  • 382
48
votes
1 answer

PostgreSQL Full Text Search and Trigram Confusion

I'm a little bit confused with the whole concept of PostgreSQL, full text search and Trigram. In my full text search queries, I'm using tsvectors, like so: SELECT * FROM articles WHERE search_vector @@ plainto_tsquery('english', 'cat, bat,…
Devin Dixon
  • 11,553
  • 24
  • 86
  • 167
48
votes
9 answers

How can I detect common substrings in a list of strings

Given a set of strings, for example: EFgreen EFgrey EntireS1 EntireS2 J27RedP1 J27GreenP1 J27RedP2 J27GreenP2 JournalP1Black JournalP1Blue JournalP1Green JournalP1Red JournalP2Black JournalP2Blue JournalP2Green I want to be able to detect that…
danio
  • 8,548
  • 6
  • 47
  • 55
47
votes
4 answers

Is there a cleaner way to pattern-match in Scala anonymous functions?

I find myself writing code like the following: val b = a map (entry => entry match { case ((x,y), u) => ((y,x), u) } ) I would like to write it differently, if only this worked: val c = a map (((x,y) -> u) => (y,x) -> u ) Is…
Owen
  • 38,836
  • 14
  • 95
  • 125
47
votes
2 answers

Redis: Find keys matching a pattern

How I can find keys matching a pattern like this: Eg: I have some keys: abc:parent1 abc:parent2 abc:parent1:child1 abc:parent2:child2 How can I find only abc:parent1 abc:parent2
Hoang Tuan
  • 507
  • 1
  • 4
  • 7
47
votes
10 answers

Pattern matching of lists in Python

I want to do some pattern matching on lists in Python. For example, in Haskell, I can do something like the following: fun (head : rest) = ... So when I pass in a list, head will be the first element, and rest will be the trailing…
mipadi
  • 398,885
  • 90
  • 523
  • 479
46
votes
3 answers

The argument types of an anonymous function must be fully known. (SLS 8.5)

I have a function literal {case QualifiedType(preds, ty) => t.ty = ty ; Some((emptyEqualityConstraintSet,preds)) } Which results in an error message missing parameter type for expanded function The argument types of an…
Theodore Norvell
  • 15,366
  • 6
  • 31
  • 45
45
votes
4 answers

How to pattern match large Scala case classes?

Consider the following Scala case class: case class WideLoad(a: String, b: Int, c: Float, d: ActorRef, e: Date) Pattern matching allows me to extract one field and discard others, like so: someVal match { case WideLoad(_, _, _, d, _) => d !…
Max A.
  • 4,842
  • 6
  • 29
  • 27