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

Defining a function in Haskell that returns true if it is the list consisting of 'a', false otherwise

I'm new to Haskell, and I'm trying to write a function that takes a list and returns a bool. It will return True if its input list is the list consisting of 'a' only, and False otherwise. This is my best guess: f :: [a] -> Bool f ('a':[]) = True f…
user2666425
  • 1,671
  • 1
  • 15
  • 21
4
votes
3 answers

Position based tuple pattern match in Haskell

Is it possible to pattern match tuples in Haskell, but without knowing the dimension of tuple? I want to create a function which mach against any tuple, which first element is A, like: data A = A Int test args@(A a,..) = a I know there is…
Wojciech Danilo
  • 11,573
  • 17
  • 66
  • 132
4
votes
2 answers

F# Pattern-matching & recursion vs looping & if..then's for parsing nested structures

I'm using a 3rd party vendor's API in F#. On initialization the API returns a C# object that is nested msg container. It is populated with status messages and may include errors message. The vendor provides a C# sample parsing routine which I have…
Andre P.
  • 613
  • 1
  • 7
  • 21
4
votes
2 answers

Querying Contacts with Partial Data

I've been trying to replicate some of the OEM dialer app behaviors regarding contact matching without much luck. Basically, I'd like to populate a list of potential contact matches as the user types numbers into the dialer that match the typed…
user888867
  • 540
  • 5
  • 16
4
votes
3 answers

Rewriting nested if-statements in a more Pythonic fashion

I'm working on a function that, given a sequence, tries to find said sequence within a list and should then return the list item immediately after that sequence terminates. Currently this code does return the list item immediately after the end of…
user2590005
4
votes
4 answers

Get a difference file by specific patterns in two text files

I have 2 text files and I need to export "changes" to a new file. That means that the second file's rows are compared to the first file's rows and if a row isn't found there, then it will append it to the new (third) file. Contents of the first…
Dropout
  • 13,653
  • 10
  • 56
  • 109
4
votes
7 answers

Extract lines with specific patterns in separate files

The input file has the following lines and segregate these using the 2nd field '+' symbol lines in one file and '-' symbol lines in another file: 24 + I am the Five man 22 - Who are you? The new number two! 51 + . . . And four on the…
doc
  • 45
  • 5
4
votes
3 answers

Remove a line with a specific pattern in one field

Below is my input file, input.txt: Value Value1 value2 5 1 2 1 4 3 2 1 5.5 0 0 0 4 1 0 I need to search the value(5.5) in the 3rd column, if found i need to remove the row…
Marjer
  • 1,313
  • 6
  • 20
  • 31
4
votes
1 answer

Pattern matching data types in Haskell. Short cuts?

In the following Haskell code, how can this be written more succinctly? Is it necessary to list all four conditions, or can these be summarized by a more compact pattern? For instance, is there a way I can take advantage of Haskell already knowing…
Steve
  • 43
  • 3
4
votes
1 answer

Writing a pattern matching macro

I need some help writing a macro that produces a pattern match. This is as far as I got: import scala.reflect.macros.Context import language.experimental.macros trait JsValue object SealedTraitFormat { def writesImpl[A: c.WeakTypeTag](c:…
0__
  • 66,707
  • 21
  • 171
  • 266
4
votes
2 answers

lua pattern matching: delimited captures

I am trying to parse a string such as: &1 first &2 second &4 fourth \\, and from it to build a table t = {1=first, 2=second, 4=fourth} I'm not very experienced with regex in general so my naive try (disregarding the \\ and table parts for the…
Scott H.
  • 143
  • 4
4
votes
3 answers

AWK print specific line based on search pattern

I have two sets of files test.csv data.xml. I am trying to grep a specific field from test.csv and search the string in data.xml. If string is found then print the corresponding line in test.csv file. Example search string is field 3 server…
Sudheej
  • 1,873
  • 6
  • 30
  • 57
4
votes
3 answers

Java regex matcher not matching

My String: FOO.BAR

Code: Pattern pattern = Pattern.compile("(browse/)(.*)(\">)"); Matcher matcher = pattern.matcher(match); return matcher.group(1); Getting error: java.lang.IllegalStateException:…
Jaanus
  • 16,161
  • 49
  • 147
  • 202
4
votes
2 answers

Pattern match on manifest instances of sealed class

Given classes sealed abstract class A case class B(param: String) extends A case class C(param: Int) extends A trait Z {} class Z1 extends Z {} class Z2 extends Z {} def zFor[T <: A : Manifest]: Option[Z] = { val z = manifest[T].erasure if…
jdevelop
  • 12,176
  • 10
  • 56
  • 112
4
votes
1 answer

How to solve logical formulas in haskell?

I am working on a haskell program that includes these data type definitions as part of it: data Term t (deriving Eq) where Con :: a -> Term a And :: Term Bool -> Term Bool -> Term Bool…
Bahar Bori
  • 41
  • 3