codesnippets:patternmatching
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| codesnippets:patternmatching [2021/04/14 12:13] – [Context: function definition] f2b216 | codesnippets: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, | ||
| + | |||
| + | ===== Wildcard ===== | ||
| + | |||
| + | * wildcard '' | ||
| + | * stands-in for any value | ||
| + | * example | ||
| + | * code:< | ||
| + | main :: IO () | ||
| + | main = | ||
| + | do | ||
| + | print (firstFromTuple3 (" | ||
| + | print (secondFromList " | ||
| + | print (getZipFromPerson (PersonNameZipAndEMail " | ||
| + | |||
| + | 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 | ||
| + | </ | ||
| + | * compiles, with warning: Pattern match(es) are non-exhaustive | ||
| + | * executes, with output:< | ||
| + | " | ||
| + | ' | ||
| + | 16802 | ||
| + | </ | ||
| + | * NOTE: The function '' | ||
| + | * example | ||
| + | * code:< | ||
| + | main :: IO () | ||
| + | main = | ||
| + | do | ||
| + | print (secondFromList " | ||
| + | |||
| + | secondFromList :: [a] -> a | ||
| + | secondFromList (_:x:_) = x | ||
| + | </ | ||
| + | * compiles, with warning: Pattern match(es) are non-exhaustive | ||
| + | * executes, with output:< | ||
| + | Test3-exe.exe: | ||
| + | </ | ||
| ===== Context: function definition ===== | ===== Context: function definition ===== | ||
| Line 23: | Line 73: | ||
| main = | main = | ||
| do | do | ||
| - | print (length s) -- " | + | print (length s) |
| s :: String | s :: String | ||
| Line 29: | Line 79: | ||
| length :: [a] -> Integer | length :: [a] -> Integer | ||
| - | length [] = 0 | + | length [] = 0 -- "inner function perspective" |
| - | length (_:lrx) = 1 + (length lrx) | + | length (_:lrx) = 1 + (length lrx) -- "inner function perspective" |
| - | -- using the rest (lrx) for recursive call of length (again " | + | |
| + | </ | ||
| + | |||
| + | |||
| + | ===== ✎ ===== | ||
| + | ~~DISCUSSION~~ | ||
codesnippets/patternmatching.1618395237.txt.gz · Last modified: (external edit)
