codesnippets:bindingandpatternmatching
This is an old revision of the document!
Table of Contents
Binding
A binding binds a name or even several names with a function definition.
In Haskell, a binding is defined by symbol “=” whereas
- on the left hand side
- the function name and its parameters
- or a pattern is given,
- 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 (=)halfis the function that can take parameterxhalfcan 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
- with compiler warning, which we ignore for pedagogical reasons
- executes, with output:
2.5
- NOTE: The term
half 5is in round brackets, which meanshalf 5has to be evaluated and its result is the first parameter of print. The expressionprint half 5would 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
- 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 the same type
- an empty list is of type
[a], whereasais 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 : []))equals3 : [2]equals[3, 2]
- example, list
- code:
[first, b, zet] = [13, 17, 19]
…works in…
module Main where main :: IO () main = do print x1 print x2 print x3 (x1, x2, x3) = (42, "Hello", "World")
Example 2
Person name number = Person "Lutz" 1243
…works in…
module Main where main :: IO () main = do print name print number data Person = Person { sName :: String, iNumber :: Integer } Person name number = Person "Lutz" 1243
Example 3
(x1, Person x2 x3, x4) = ("...", Person "Lutz" 1243, '#')
…works in…
module Main where main :: IO () main = do print x1 print x2 print x3 print x4 data Person = Person { sName :: String, iNumber :: Integer } (x1, Person x2 x3, x4) = ("...", Person "Lutz" 1243, '#')
You could leave a comment if you were logged in.
codesnippets/bindingandpatternmatching.1618345082.txt.gz · Last modified: (external edit)
