User Tools

Site Tools


codesnippets:patternmatching

Differences

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

Link to this comparison view

Next revision
Previous revision
codesnippets:patternmatching [2021/04/14 12:00] – created f2b216codesnippets:patternmatching [2025/10/08 00:48] (current) – external edit 127.0.0.1
Line 5: Line 5:
   * list comprehensions   * list comprehensions
   * do notations   * do notations
 +  * case constructs
  
 Before we start with that, we provide more details about pattern matching, regarding: Before we start with that, we provide more details about pattern matching, regarding:
Line 10: Line 11:
   * algebraic data types   * algebraic data types
   * tuples   * tuples
 +
 +Additionally, even before that we explain the wildcard ''_''.
 +
 +===== Wildcard =====
 +
 +  * wildcard ''_''
 +    * stands-in for any value
 +  * example
 +    * code:<code Hsakell>
 +main :: IO ()
 +main = 
 +    do
 +        print (firstFromTuple3 ("A",'b','c'))
 +        print (secondFromList "Hallo!")
 +        print (getZipFromPerson (PersonNameZipAndEMail "Haskell" "Brooks" "Curry" 16802 "hbc@psu.edu"))
 +
 +firstFromTuple3 :: (a,b,c) -> a
 +firstFromTuple3 (x,_,_) = x
 +
 +secondFromList :: [a] -> a
 +secondFromList (_:x:_) = x
 +
 +data Person = PersonNameAndZip String String String Int | PersonNameZipAndEMail String String String Int String
 +
 +getZipFromPerson :: Person -> Int
 +getZipFromPerson (PersonNameAndZip _ _ _ nZip) = nZip
 +getZipFromPerson (PersonNameZipAndEMail _ _ _ nZip _) = nZip
 +</code>
 +    * compiles, with warning: Pattern match(es) are non-exhaustive
 +    * executes, with output:<code>
 +"A"
 +'a'
 +16802
 +</code>
 +  * NOTE: The function ''secondFromList'' will execute with runtine error when there is less than two elements in the list.
 +  * example
 +    * code:<code Hsakell>
 +main :: IO ()
 +main = 
 +    do
 +        print (secondFromList "H")
 +
 +secondFromList :: [a] -> a
 +secondFromList (_:x:_) = x
 +</code>
 +    * compiles, with warning: Pattern match(es) are non-exhaustive
 +    * executes, with output:<code>
 +Test3-exe.exe: app\Main.hs:7:1-26: Non-exhaustive patterns in function secondFromList
 +</code>
  
 ===== Context: function definition ===== ===== Context: function definition =====
Line 23: Line 73:
 main =  main = 
     do     do
-        print (length s)+        print (length s)             -- "caller perspective" for first parameter of function length
  
 s :: String s :: String
Line 29: Line 79:
  
 length :: [a] -> Integer length :: [a] -> Integer
-length [] = 0 +length [] = 0                        -- "inner function perspective" - selected when list is empty 
-length (_:lrx) = 1 + (length lrx)+length (_:lrx) = 1 + (length lrx)    -- "inner function perspective" - selected when at least one element 
 +                                     -- using the rest (lrx) for recursive call of length (again "caller perspective")
 </code> </code>
 +
 +
 +===== ✎ =====
 +~~DISCUSSION~~
codesnippets/patternmatching.1618394428.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