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
3 answers

Discriminated union pattern matching by function call

My question is inspired by this one: link Here is a code: type A = | X of int * int | Y of string let f (A.X(a, b)) = a + b It works, but with a warning: Makes sense; I have no match for Y. But if I add a line let…
Rustam
  • 1,766
  • 18
  • 34
4
votes
3 answers

How can I match a function signature without getting type erasure compiler warnings in Scala

Can anyone re-write this code to do the same thing but without any compiler warnings please:- object TestTypeErasure { def main(args:Array[String]) { def myFunction(myObject:Any):Boolean = { true } val myVariable: (Any =>…
4
votes
2 answers

Why doesn't this pattern matching work as expected in Scala?

I'm trying to replicate the powerful pattern matching example that Joshua Suereth presented in his Devoxx 2013 talk titled "How to wield Scala in the trenches". Unfortunately I cannot achieve what he described and I cannot understand what is wrong.…
Emre Sevinç
  • 8,211
  • 14
  • 64
  • 105
4
votes
7 answers

Exporting specific pattern of string using split method in a most efficient way

I want to export pattern of bit stream in a String varilable. Assume our bit stream is something like bitStream="111000001010000100001111". I am looking for a Java code to save this bit stream in a specific array (assume bitArray) in a way that all…
Ali
  • 1,759
  • 2
  • 32
  • 69
4
votes
1 answer

Matching sub-classes of case classes in Scala

Why does this fail to compile (or work?): case class A(x: Int) class B extends A(5) (new B) match { case A(_) => println("found A") case _ => println("something else happened?") } The compiler error is: constructor cannot be…
Mitch Blevins
  • 13,186
  • 3
  • 44
  • 32
4
votes
1 answer

How to order and print string cells content by the match within the individual strings, Matlab?

I want to print the content of a cell line such that matching strings are one after another. The original line looks like: Example 1: 'E11E81' 'E21E81' 'E31E51' 'E31E61' 'E61E81' From this line, I would like to print: E11 - E81 - E61 -…
Aquila
  • 65
  • 1
  • 7
4
votes
5 answers

Find sequences of digits in long integers efficiently

Is it possible to find a defined sequence in an integer without converting it to a string? That is, is it possible to do some form of pattern matching directly on integers. I have not thought of one but I keeping thinking there should be a…
Vincent
  • 1,579
  • 4
  • 23
  • 38
4
votes
3 answers

Scala pattern match multiple types

I have a code for extracting Int from JValue that should look exactly the same for multiple JValue subclasses, so I'm trying to avoid repeating myself. However, as it is (see below), scala thinks that j is a generic JValue, and that j.values returns…
Yar
  • 629
  • 6
  • 17
4
votes
3 answers

Haskell: Rigid type variable error when pattern matching bind operator

I'm trying to run this newtype Test a = Test (Int, a) instance Monad Test where Test (_, []) >>= k = k [] Test (_, a) >>= k = k a return a = Test (0, a) And I get the error: Couldn't match expected type `a' with…
Zantier
  • 833
  • 1
  • 8
  • 18
4
votes
2 answers

Is | (or) short circuited when pattern matching in scala?

I noticed that there is no || operator available when pattern matching - is | short circuited?
JasonG
  • 5,794
  • 4
  • 39
  • 67
4
votes
9 answers

Image Classification - Detecting Floor Plans

I am working on a real estate website and i would like to write a program that can figure out(classify) if an image is a floor plan or a company logo. Since i am writing in php i will prefer a php solution but any c++ or opencv solution will be fine…
4
votes
2 answers

mlpy - Dynamic Time Warping depends on x?

I am trying to get the distance between these two arrays shown below by DTW. I am using the Python mlpy package that offers dist, cost, path = mlpy.dtw_std(y1, y2, dist_only=False) I understand that DTW does take care of the "shifting". In…
Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174
4
votes
4 answers

Type Matching in Haskell

If SomeType is defined as: data SomeType = X {myBool :: Bool} | Y {myString :: String} | Z {myString :: String} and I will update an arbitrary X, dependent of his type as follows: changeST :: SomeType ->…
ChrisQuignon
  • 685
  • 1
  • 6
  • 9
4
votes
1 answer

How can I simplify this ocaml pattern-matching code?

I'm writing a simple little ocaml program that reads an algebraic statement in from a file, parses it into an AST using ocamllex/ocamlyacc, reduces it, and then prints it. The part where I'm reducing the expression seems a bit... ugly. Is there…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
4
votes
3 answers

Can I make Pattern Password Screen Locker android app

Has anyone got experience with developing an application to replace the default lock screen? I've been told it is not possible, however this application manages it. Any tutorials or guidance you know of would be appreciated.
Nada Feteiha
  • 61
  • 1
  • 2
  • 7