What is 'Pattern Matching' in functional languages?

后端 未结 9 439
栀梦
栀梦 2020-11-30 16:43

I\'m reading about functional programming and I\'ve noticed that Pattern Matching is mentioned in many articles as one of the core features of functional languages.

相关标签:
9条回答
  • 2020-11-30 17:19

    For many people, picking up a new concept is easier if some easy examples are provided, so here we go:

    Let's say you have a list of three integers, and wanted to add the first and the third element. Without pattern matching, you could do it like this (examples in Haskell):

    Prelude> let is = [1,2,3]
    Prelude> head is + is !! 2
    4
    

    Now, although this is a toy example, imagine we would like to bind the first and third integer to variables and sum them:

    addFirstAndThird is =
        let first = head is
            third = is !! 3
        in first + third
    

    This extraction of values from a data structure is what pattern matching does. You basically "mirror" the structure of something, giving variables to bind for the places of interest:

    addFirstAndThird [first,_,third] = first + third
    

    When you call this function with [1,2,3] as its argument, [1,2,3] will be unified with [first,_,third], binding first to 1, third to 3 and discarding 2 (_ is a placeholder for things you don't care about).

    Now, if you only wanted to match lists with 2 as the second element, you can do it like this:

    addFirstAndThird [first,2,third] = first + third
    

    This will only work for lists with 2 as their second element and throw an exception otherwise, because no definition for addFirstAndThird is given for non-matching lists.

    Until now, we used pattern matching only for destructuring binding. Above that, you can give multiple definitions of the same function, where the first matching definition is used, thus, pattern matching is a little like "a switch statement on stereoids":

    addFirstAndThird [first,2,third] = first + third
    addFirstAndThird _ = 0
    

    addFirstAndThird will happily add the first and third element of lists with 2 as their second element, and otherwise "fall through" and "return" 0. This "switch-like" functionality can not only be used in function definitions, e.g.:

    Prelude> case [1,3,3] of [a,2,c] -> a+c; _ -> 0
    0
    Prelude> case [1,2,3] of [a,2,c] -> a+c; _ -> 0
    4
    

    Further, it is not restricted to lists, but can be used with other types as well, for example matching the Just and Nothing value constructors of the Maybe type in order to "unwrap" the value:

    Prelude> case (Just 1) of (Just x) -> succ x; Nothing -> 0
    2
    Prelude> case Nothing of (Just x) -> succ x; Nothing -> 0
    0
    

    Sure, those were mere toy examples, and I did not even try to give a formal or exhaustive explanation, but they should suffice to grasp the basic concept.

    0 讨论(0)
  • 2020-11-30 17:24

    Short answer: Pattern matching arises because functional languages treat the equals sign as an assertion of equivalence instead of assignment.

    Long answer: Pattern matching is a form of dispatch based on the “shape” of the value that it's given. In a functional language, the datatypes that you define are usually what are known as discriminated unions or algebraic data types. For instance, what's a (linked) list? A linked list List of things of some type a is either the empty list Nil or some element of type a Consed onto a List a (a list of as). In Haskell (the functional language I'm most familiar with), we write this

    data List a = Nil
                | Cons a (List a)
    

    All discriminated unions are defined this way: a single type has a fixed number of different ways to create it; the creators, like Nil and Cons here, are called constructors. This means that a value of the type List a could have been created with two different constructors—it could have two different shapes. So suppose we want to write a head function to get the first element of the list. In Haskell, we would write this as

    -- `head` is a function from a `List a` to an `a`.
    head :: List a -> a
    -- An empty list has no first item, so we raise an error.
    head Nil        = error "empty list"
    -- If we are given a `Cons`, we only want the first part; that's the list's head.
    head (Cons h _) = h
    

    Since List a values can be of two different kinds, we need to handle each one separately; this is the pattern matching. In head x, if x matches the pattern Nil, then we run the first case; if it matches the pattern Cons h _, we run the second.

    Short answer, explained: I think one of the best ways to think about this behavior is by changing how you think of the equals sign. In the curly-bracket languages, by and large, = denotes assignment: a = b means “make a into b.” In a lot of functional languages, however, = denotes an assertion of equality: let Cons a (Cons b Nil) = frob x asserts that the thing on the left, Cons a (Cons b Nil), is equivalent to the thing on the right, frob x; in addition, all variables used on the left become visible. This is also what's happening with function arguments: we assert that the first argument looks like Nil, and if it doesn't, we keep checking.

    0 讨论(0)
  • 2020-11-30 17:26

    Pattern matching is where the interpreter for your language will pick a particular function based on the structure and content of the arguments you give it.

    It is not only a functional language feature but is available for many different languages.

    The first time I came across the idea was when I learned prolog where it is really central to the language.

    e.g.

    last([LastItem], LastItem).

    last([Head|Tail], LastItem) :- last(Tail, LastItem).

    The above code will give the last item of a list. The input arg is the first and the result is the second.

    If there is only one item in the list the interpreter will pick the first version and the second argument will be set to equal the first i.e. a value will be assigned to the result.

    If the list has both a head and a tail the interpreter will pick the second version and recurse until it there is only one item left in the list.

    0 讨论(0)
提交回复
热议问题