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

What is the command to match brackets in Emacs?

What is the command to match brackets in Emacs (the equivalent of the % command in Vim)?
Chris Huang-Leaver
  • 6,059
  • 6
  • 41
  • 67
45
votes
5 answers

Haskell pattern matching - what is it?

What is pattern matching in Haskell and how is it related to guarded equations? I've tried looking for a simple explanation, but I haven't found one. EDIT: Someone tagged as homework. I don't go to school anymore, I'm just learning Haskell and I'm…
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
45
votes
5 answers

KMP prefix table

I am reading about KMP for string matching. It needs a preprocessing of the pattern by building a prefix table. For example for the string ababaca the prefix table is: P = [0, 0, 1, 2, 3, 0, 1] But I am not clear on what does the numbers show. I…
Cratylus
  • 52,998
  • 69
  • 209
  • 339
44
votes
6 answers

Scala: short form of pattern matching that returns Boolean

I found myself writing something like this quite often: a match { case `b` => // do stuff case _ => // do nothing } Is there a shorter way to check if some value matches a pattern? I mean, in this case I could just write if (a == b) // do…
Vilius Normantas
  • 3,708
  • 6
  • 25
  • 38
43
votes
2 answers

What is scala's experimental virtual pattern matcher?

I've seen quite a few mentions recently of the new "virtualized" pattern matcher for scala. I missed the memo explaining what it actually was...
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
43
votes
3 answers

What is the difference between unapply and unapplySeq?

Why does Scala have both unapply and unapplySeq? What is the difference between the two? When should I prefer one over the other?
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
43
votes
2 answers

What is the syntactical equivalent to switch/case in Python?

Programming languages like C/C++, C#, Java, JavaScript and Pascal (Reference) have a combination of switch and case statements (sometimes also called select or inspect) which allow you to check one value against several conditions to perform certain…
43
votes
2 answers

How do I match multiple arguments?

I have a function: def func(a: int, b: int, c: double): int And I want to match various possible scenarios Wherever c is 0, return b-a Wherever c > 9, return 0 Wherever a=b return 0 And so on, before doing some more complex logic if none of the…
Phil H
  • 19,928
  • 7
  • 68
  • 105
43
votes
1 answer

What is Axiom K?

I've noticed the discussion of "Axiom K" comes up more often since HoTT. I believe it's related to pattern matching. I'm surprised that I cannot find a reference in TAPL, ATTAPL or PFPL. What is Axiom K? Is it used for ML-style pattern matching as…
Steven Shaw
  • 6,063
  • 3
  • 33
  • 44
43
votes
2 answers

Regular expression in PostgreSQL LIKE clause

I'm stuck with a simple regular expression. Not sure what I'm missing. A little rusty on regex skills. The expression I'm trying to match is: select * from table where value like '00[1-9]%' -- (third character should not be 0) So this should match…
borarak
  • 1,130
  • 1
  • 13
  • 24
42
votes
6 answers

Pattern matching using a wildcard

How do I identify a string using a wildcard? I've found glob2rx, but I don't quite understand how to use it. I tried using the following code to pick the rows of the data frame that begin with the word blue: # make data frame a <- data.frame( x = …
djq
  • 14,810
  • 45
  • 122
  • 157
42
votes
2 answers

Expected String, found &str when matching an optional string

I am trying to write a simple function in Rust that will ask user a question expecting answer of "you" or "me". It should return a boolean value or ask again if the user answers wrong. I came up with: fn player_starts() -> bool { …
zefciu
  • 1,967
  • 2
  • 17
  • 39
41
votes
4 answers

How can I pattern match on a range in Scala?

In Ruby I can write this: case n when 0...5 then "less than five" when 5...10 then "less than ten" else "a lot" end How do I do this in Scala? Edit: preferably I'd like to do it more elegantly than using if.
Theo
  • 131,503
  • 21
  • 160
  • 205
41
votes
4 answers

Scala: Ignore case class field for equals/hascode?

Is it possible to ignore a field of a case class in the equals/haschode method of the case class? My use case is that I have a field that is essentially metadata for rest of the data in the class.
ChucK
  • 2,114
  • 1
  • 18
  • 20
40
votes
3 answers

What does let 5 = 10 do? Is it not an assignment operation?

If I say let 5 = 10, why does 5 + 1 return 6 instead of 11?
user2763173