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/14 11:06] – [Tuples] 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 133: Line 132:
     * but with binding names     * but with binding names
   * example   * example
-  *  
     * code lines:<code Haskell>     * code lines:<code Haskell>
 data MyData = MyVariantA Integer Char Double | MyVariantB Int | MyVariantC data MyData = MyVariantA Integer Char Double | MyVariantB Int | MyVariantC
Line 145: Line 143:
         print b         print b
         print zet         print zet
-        print n 
  
 data MyData = MyVariantA Integer Char Double | MyVariantB Int | MyVariantC data MyData = MyVariantA Integer Char Double | MyVariantB Int | MyVariantC
-    deriving Show 
  
 MyVariantA first b zet = MyVariantA 12 'a' 23.4 MyVariantA first b zet = MyVariantA 12 'a' 23.4
Line 154: Line 150:
 MyVariantB n = MyVariantB 123 MyVariantB n = MyVariantB 123
  
-MyVariantC = MyVariantC+MyVariantC = MyVariantC -- is not binding anything, but compiles consistently
 </code> </code>
     * with compiler warning, which we ignore for pedagogical reasons     * with compiler warning, which we ignore for pedagogical reasons
Line 163: Line 159:
 123 123
 </code> </code>
- +  * NOTE: I may look like: It does'nt make sense to have a constructor no elementsHowever, ''MyVariantC'' is already a value in itself, and can be used to differentiate between the variants.
-==== Example 2 ==== +
- +
-<code Haskell> +
-Person name number = Person "Lutz" 1243 +
-</code> +
- +
-...works in... +
- +
-<code Haskell> +
-module Main where +
- +
-main :: IO () +
-main =  +
-        do +
-                print name +
-                print number +
- +
-data Person = Person { sName :: String, iNumber :: Integer } +
- +
-Person name number = Person "Lutz" 1243 +
-</code>+
  
  
 +===== ✎ =====
 +~~DISCUSSION~~
codesnippets/bindingandpatternmatching.1618391180.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