User Tools

Site Tools


codesnippets:bindingandpatternmatching

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
codesnippets:bindingandpatternmatching [2021/04/13 22:16] f2b216codesnippets:bindingandpatternmatching [2025/10/08 00:48] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Binding ====== +====== Binding and pattern matching ======
-~~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 46: Line 45:
 ==== Lists ==== ==== Lists ====
  
-  * lists can be written in the form ''['' <elem 1>, <elem 2>, ... <elem n>'']'' +  * list construction 
-    * whereas ''[]'' is an empty list +    * lists can be written in the form ''['' <elem 1>, <elem 2>, ... <elem n>'']'' 
-    * the elements between brackets are comma seperated +      * whereas ''[]'' is an empty list 
-  * all elements of a lists have the same type +      * the elements between brackets are comma seperated 
-  * an empty list is of type ''[a]'', whereas ''a'' is a type parameter +    * all elements of a lists have to have the same type 
-  * lists can also be written in the form  <elem 1> : <elem 2> : ... <elem n> : ''['' '']'' +    * list can be defined infinite 
-    * '':'' is an operator that appends an element on the left hand side to a list on the right hand side +    * an empty list is of type ''[a]'', whereas ''a'' is a type parameter 
-    * '':'' is associated to the right +    * lists can also be written in the form  <elem 1> : <elem 2> : ... <elem n> : ''['' '']'' 
-    * e.g. ''3 : 2 : []'' equals ''(3 : (2 : []))'' +      * '':'' is an operator that appends an element on the left hand side to a list on the right hand side 
-  * lists can be written in a mixed form  <elem 1> : <elem 2> : ... <elem n> : ''['' <elem n+1>, <elem n+2>, ... <elem m>'']'' +      * '':'' is associated to the right 
-    * e.g. ''(3 : (2 : []))'' equals ''3 : [2]'' equals ''[3, 2]'' +      * 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>'']'' 
-<code Haskell> +      * e.g. ''(3 : (2 : []))'' equals ''3 : [2]'' equals ''[3, 2]'' 
-(x1x2x3) (42"Hello""World")+  * pattern maching with lists 
 +    * in the same form as thay are constructed above 
 +    * but with binding names 
 +  * example 
 +    * code line:<code Haskell> 
 +[firstbzet] [131719]
 </code> </code>
- +    * works in:<code Haskell>
-...works in... +
- +
-<code Haskell> +
-module Main where +
 main :: IO () main :: IO ()
 main =  main = 
-        do +    do 
-                print x1 +        print first 
-                print x2 +        print b 
-                print x3+        print zet
  
-(x1x2x3) (42"Hello""World")+[firstbzet] [131719]
 </code> </code>
- +    * with compiler warning, which we ignore for pedagogical reasons 
-==== Example 2 ==== +    * executes, with output:<code> 
- +13 
-<code Haskell+17 
-Person name number = Person "Lutz" 1243+19
 </code> </code>
  
-...works in... +==== Tuples ====
- +
-<code Haskell> +
-module Main where+
  
 +  * 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:<code Haskell>
 +[first, b, zet] = [13, 17, 19]
 +</code>
 +    * works in:<code Haskell>
 main :: IO () main :: IO ()
 main =  main = 
-        do +    do 
-                print name +        print first 
-                print number+        print 
 +        print zet
  
-data Person = Person { sName :: StringiNumber :: Integer } +(firstb, zet) ('a',12,23.4)
- +
-Person name number Person "Lutz" 1243+
 </code> </code>
- +    * with compiler warning, which we ignore for pedagogical reasons 
-==== Example 3 ==== +    * executes, with output:<code> 
- +'a' 
-<code Haskell+12   
-(x1, Person x2 x3, x4) = ("...", Person "Lutz" 1243, '#')+23.4
 </code> </code>
  
-...works in... +==== Algebraic data types ====
- +
-<code Haskell> +
-module Main where+
  
 +  * 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:<code Haskell>
 +data MyData = MyVariantA Integer Char Double | MyVariantB Int | MyVariantC
 +MyVariantA first b zet = MyVariantA 12 'a' 23.4
 +</code>
 +    * work in:<code Haskell>
 main :: IO () main :: IO ()
 main =  main = 
-        do +    do 
-                print x1 +        print first 
-                print x2 +        print b 
-                print x3 +        print zet
-                print x4+
  
-data Person Person { sName :: String, iNumber :: Integer }+data MyData MyVariantA Integer Char Double | MyVariantB Int | MyVariantC
  
-(x1, Person x2 x3, x4) ("...", Person "Lutz" 1243, '#'+MyVariantA first b zet MyVariantA 12 'a23.4
-</code>+
  
 +MyVariantB n = MyVariantB 123
  
 +MyVariantC = MyVariantC -- is not binding anything, but compiles consistently
 +</code>
 +    * with compiler warning, which we ignore for pedagogical reasons
 +    * executes, with output:<code>
 +12
 +'a'
 +23.4
 +123
 +</code>
 +  * 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.
  
 +
 +===== ✎ =====
 +~~DISCUSSION~~
codesnippets/bindingandpatternmatching.1618344990.txt.gz · Last modified: (external edit)

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