codesnippets:patternmatching

Pattern matching, again

There was already some pattern matching explained, in the context of bindings here. Here we explain pattern matching in other contexts, which are:

  • function definitions
  • list comprehensions
  • do notations
  • case constructs

Before we start with that, we provide more details about pattern matching, regarding:

  • lists
  • algebraic data types
  • tuples

Additionally, even before that we explain the wildcard _.

Wildcard

  • wildcard _
    • stands-in for any value
  • example
    • code:
      main :: IO ()
      main = 
          do
              print (firstFromTuple3 ("A",'b','c'))
              print (secondFromList "Hallo!")
              print (getZipFromPerson (PersonNameZipAndEMail "Haskell" "Brooks" "Curry" 16802 "hbc@psu.edu"))
       
      firstFromTuple3 :: (a,b,c) -> a
      firstFromTuple3 (x,_,_) = x
       
      secondFromList :: [a] -> a
      secondFromList (_:x:_) = x
       
      data Person = PersonNameAndZip String String String Int | PersonNameZipAndEMail String String String Int String
       
      getZipFromPerson :: Person -> Int
      getZipFromPerson (PersonNameAndZip _ _ _ nZip) = nZip
      getZipFromPerson (PersonNameZipAndEMail _ _ _ nZip _) = nZip
    • compiles, with warning: Pattern match(es) are non-exhaustive
    • executes, with output:
      "A"
      'a'
      16802
  • NOTE: The function secondFromList will execute with runtine error when there is less than two elements in the list.
  • example
    • code:
      main :: IO ()
      main = 
          do
              print (secondFromList "H")
       
      secondFromList :: [a] -> a
      secondFromList (_:x:_) = x
    • compiles, with warning: Pattern match(es) are non-exhaustive
    • executes, with output:
      Test3-exe.exe: app\Main.hs:7:1-26: Non-exhaustive patterns in function secondFromList

Context: function definition

  • binding actual parameters (“caller perspective”) to parameters (“inner function perspective”)
  • select the structure of data, to pick cases
  • binds parameter names, to define the function itself
  • example:
    • code:
      import Prelude hiding (length)
       
      main :: IO ()
      main = 
          do
              print (length s)             -- "caller perspective" for first parameter of function length
       
      s :: String
      s = ('H':'e':['l', 'l', 'o']) ++ (' ':"World!")
       
      length :: [a] -> Integer
      length [] = 0                        -- "inner function perspective" - selected when list is empty
      length (_:lrx) = 1 + (length lrx)    -- "inner function perspective" - selected when at least one element
                                           -- using the rest (lrx) for recursive call of length (again "caller perspective")

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
You could leave a comment if you were logged in.
codesnippets/patternmatching.txt · Last modified: by 127.0.0.1

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki