codesnippets:bindingandpatternmatching
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| codesnippets:bindingandpatternmatching [2021/04/13 21:40] – [a) with function name and parameter] f2b216 | codesnippets:bindingandpatternmatching [2025/10/08 00:48] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Binding ====== | + | ====== Binding |
| - | ~~DISCUSSION~~ | + | |
| A binding binds a name or even several names with a function definition. | A binding binds a name or even several names with a function definition. | ||
| Line 13: | Line 12: | ||
| * compile, error and warning free, with compiler: GHC 8.10.4, using compiler option -Wall | * compile, error and warning free, with compiler: GHC 8.10.4, using compiler option -Wall | ||
| - | ===== a) with function name and parameter ===== | + | ===== Binding with a) function name and parameter ===== |
| * function name an parameter ('' | * function name an parameter ('' | ||
| Line 27: | Line 26: | ||
| main :: IO () | main :: IO () | ||
| main = | main = | ||
| - | | + | |
| - | print $ half 5 | + | print (half 5) |
| - | + | ||
| - | default (Double) | + | |
| half x = x / 2 | half x = x / 2 | ||
| Line 38: | Line 35: | ||
| 2.5 | 2.5 | ||
| </ | </ | ||
| + | * NOTE: The term '' | ||
| - | ===== 2) with pattern matching ===== | + | ===== Binding |
| - | ==== Example 1 ==== | + | * 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__** | ||
| - | <code Haskell> | + | ==== Lists ==== |
| - | (x1, x2, x3) = (42, " | + | |
| - | </ | + | |
| - | + | ||
| - | ...works in... | + | |
| - | + | ||
| - | <code Haskell> | + | |
| - | module Main where | + | |
| + | * list construction | ||
| + | * lists can be written in the form '' | ||
| + | * whereas '' | ||
| + | * 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 '' | ||
| + | * lists can also be written in the form <elem 1> : <elem 2> : ... <elem n> : '' | ||
| + | * '':'' | ||
| + | * '':'' | ||
| + | * e.g. '' | ||
| + | * lists can be written in a mixed form <elem 1> : <elem 2> : ... <elem n> : '' | ||
| + | * e.g. '' | ||
| + | * 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:<code Haskell> | ||
| main :: IO () | main :: IO () | ||
| main = | main = | ||
| - | | + | |
| - | print x1 | + | print first |
| - | print x2 | + | print b |
| - | print x3 | + | print zet |
| - | (x1, x2, x3) = (42, " | + | [first, b, zet] = [13, 17, 19] |
| </ | </ | ||
| - | + | * with compiler warning, which we ignore for pedagogical reasons | |
| - | ==== Example 2 ==== | + | * executes, with output:< |
| - | + | 13 | |
| - | < | + | 17 |
| - | Person name number = Person " | + | 19 |
| </ | </ | ||
| - | ...works in... | + | ==== Tuples ==== |
| - | + | ||
| - | <code Haskell> | + | |
| - | module Main where | + | |
| + | * tuple construction | ||
| + | * tuples are written in the form '' | ||
| + | * 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:<code Haskell> | ||
| main :: IO () | main :: IO () | ||
| main = | main = | ||
| - | | + | |
| - | print name | + | print first |
| - | print number | + | print b |
| + | print zet | ||
| - | data Person = Person { sName :: String, iNumber :: Integer } | + | (first, b, zet) = (' |
| - | + | ||
| - | Person name number | + | |
| </ | </ | ||
| - | + | * with compiler warning, which we ignore for pedagogical reasons | |
| - | ==== Example 3 ==== | + | * executes, with output:< |
| - | + | 'a' | |
| - | < | + | 12 |
| - | (x1, Person x2 x3, x4) = (" | + | 23.4 |
| </ | </ | ||
| - | ...works in... | + | ==== Algebraic data types ==== |
| - | + | ||
| - | <code Haskell> | + | |
| - | module Main where | + | |
| + | * type definitions | ||
| + | * algebraic data types can be defined in the form '' | ||
| + | * whereas '' | ||
| + | * 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 < | ||
| + | * 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 ' | ||
| + | </ | ||
| + | * work in:<code Haskell> | ||
| main :: IO () | main :: IO () | ||
| main = | main = | ||
| - | | + | |
| - | print x1 | + | print first |
| - | print x2 | + | print b |
| - | print x3 | + | print zet |
| - | print x4 | + | |
| - | data Person | + | data MyData |
| - | (x1, Person x2 x3, x4) = (" | + | MyVariantA first b zet = MyVariantA 12 'a' |
| - | </ | + | |
| + | 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 | ||
| + | ' | ||
| + | 23.4 | ||
| + | 123 | ||
| + | </ | ||
| + | * NOTE: I may look like: It does' | ||
| + | |||
| + | ===== ✎ ===== | ||
| + | ~~DISCUSSION~~ | ||
codesnippets/bindingandpatternmatching.1618342816.txt.gz · Last modified: (external edit)
