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/14 10:43] – [Tuples] 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 119: | Line 118: | ||
| </ | </ | ||
| - | ==== Example 2 ==== | + | ==== Algebraic data types ==== |
| - | <code Haskell> | + | * type definitions |
| - | Person name number | + | * 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:<code Haskell> | ||
| + | data MyData = MyVariantA Integer Char Double | MyVariantB Int | MyVariantC | ||
| + | MyVariantA first b zet = MyVariantA 12 ' | ||
| </ | </ | ||
| - | + | * work in:<code Haskell> | |
| - | ...works | + | |
| - | + | ||
| - | <code Haskell> | + | |
| - | module Main where | + | |
| main :: IO () | main :: IO () | ||
| main = | main = | ||
| - | | + | |
| - | print name | + | print first |
| - | print number | + | print b |
| + | print zet | ||
| - | data Person | + | data MyData |
| - | Person name number | + | MyVariantA first b zet = MyVariantA 12 ' |
| + | |||
| + | 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.1618389837.txt.gz · Last modified: (external edit)
