User Tools

Site Tools


codesnippets:bindingandpatternmatching

Binding and pattern matching

A binding binds a name or even several names with a function definition.

In Haskell, a binding is defined by symbol “=” whereas

  1. on the left hand side
    1. the function name and its parameters
    2. or a pattern is given,
  2. and on the right had side the definition of the function.
  • examples
    • compile, error and warning free, with compiler: GHC 8.10.4, using compiler option -Wall

Binding with a) function name and parameter

  • function name an parameter (half x), on the left hand side of the equal sign (=)
    • half is the function that can take parameter x
    • half can be evaluated by name with a parameter just right behind the function name (e.g. half 5)
  • function definition (x / 2), on the right hand side
    • if half is demanded to be evaluated then the result is delegated to function (/) with parameter x and 2
  • example
    • code line:
      half x = x / 2
    • works in:
      main :: IO ()
      main = 
          do
                  print (half 5)
       
      half x = x / 2
    • with compiler warning, which we ignore for pedagogical reasons
    • executes, with output:
      2.5
    • NOTE: The term half 5 is in round brackets, which means half 5 has to be evaluated and its result is the first parameter of print. The expression print half 5 would lead to a compiler error, beause this would be interpreted as print has two parameters.

Binding with b) pattern matching

  • pattern matching needs the knowledge of tuples, arrays, and algebraic data types
  • unfortunately tuples, arrays, and algebraic data types can only be fully understood with pattern matching
  • this is attempt to explain both at the same time, while keeping the focus on bindings

Lists

  • list construction
    • lists can be written in the form [ <elem 1>, <elem 2>, … <elem n>]
      • whereas [] is an empty list
      • the elements between brackets are comma seperated
    • all elements of a lists have to have the same type
    • list can be defined infinite
    • an empty list is of type [a], whereas a is a type parameter
    • lists can also be written in the form <elem 1> : <elem 2> : … <elem n> : [ ]
      • : is an operator that appends an element on the left hand side to a list on the right hand side
      • : is associated to the right
      • e.g. 3 : 2 : [] equals (3 : (2 : []))
    • lists can be written in a mixed form <elem 1> : <elem 2> : … <elem n> : [ <elem n+1>, <elem n+2>, … <elem m>]
      • e.g. (3 : (2 : [])) equals 3 : [2] equals [3, 2]
  • pattern maching with lists
    • in the same form as thay are constructed above
    • but with binding names
  • example
    • code line:
      [first, b, zet] = [13, 17, 19]
    • works in:
      main :: IO ()
      main = 
          do
              print first
              print b
              print zet
       
      [first, b, zet] = [13, 17, 19]
    • with compiler warning, which we ignore for pedagogical reasons
    • executes, with output:
      13
      17
      19

Tuples

  • tuple construction
    • tuples are written in the form ( <elem 1>, <elem 2>, … <elem n>)
      • elements between brackets are comma seperated
    • elements of a tuple can have different types
    • tuples can contain two or more elements
      • cannot be empty
      • cannot have a single element only
    • the type of the tuple is the tuple with the order of types e.g. (Char, Int, Double)
    • elements of tuples are finite
  • pattern maching with tuples
    • in the same form as thay are constructed above
    • but with binding names
  • example
    • code line:
      [first, b, zet] = [13, 17, 19]
    • works in:
      main :: IO ()
      main = 
          do
              print first
              print b
              print zet
       
      (first, b, zet) = ('a',12,23.4)
    • with compiler warning, which we ignore for pedagogical reasons
    • executes, with output:
      'a'
      12  
      23.4

Algebraic data types

  • type definitions
    • algebraic data types can be defined in the form data <TypeName> = <DataConstructorA> <DataTypeA1> <DataTypeA2> … <DataTypeAN> | <DataConstructorB> <DataTypeB1> <DataTypeB2> … <DataTypeBM> | <DataConstructorO> …
    • whereas | devides variants simmilar to variants in unions in programming the language C and C++
    • each variant has to have a constructor
    • each constructor can contain zero, one or more data types
  • construction of data of algebraic data types
    • constructed in the form <DataConstructor> <Value1> <Value2> … <ValueN>
    • whereas the values have to conform with the data type declaration
  • pattern maching with tuples
    • in the same form as thay are constructed above
    • but with binding names
  • example
    • code lines:
      data MyData = MyVariantA Integer Char Double | MyVariantB Int | MyVariantC
      MyVariantA first b zet = MyVariantA 12 'a' 23.4
    • work in:
      main :: IO ()
      main = 
          do
              print first
              print b
              print zet
       
      data MyData = MyVariantA Integer Char Double | MyVariantB Int | MyVariantC
       
      MyVariantA first b zet = MyVariantA 12 'a' 23.4
       
      MyVariantB n = MyVariantB 123
       
      MyVariantC = MyVariantC -- is not binding anything, but compiles consistently
    • with compiler warning, which we ignore for pedagogical reasons
    • executes, with output:
      12
      'a'
      23.4
      123
  • NOTE: I may look like: It does'nt make sense to have a constructor no elements. However, MyVariantC is already a value in itself, and can be used to differentiate between the variants.

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/bindingandpatternmatching.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