If You Must Learn Haskall ( Part 2 )
Haskell checks type at compile time, preventing programs from crashing.
Recall: Haskell has Type Inference.
Allowing us to write general functions that work for arbitrary types !
Pattern Matching
Pattern Matching is essentially of specifying patterns to which some data should conform and then checking to see if it does and deconstructing the data according to those patterns !!
Then Racket's Pattern Matching
This leads to very neat code that's very simple and readable.
You can Pattern Match on any data type.
The Patterns are checked from top to bottom, when it meets a pattern that works, the corresponding function body will be used.
Basically a collection of functions, that run depending on what pattern the input is in.
charName :: Char -> String
charName 'a' = "Albert"
charName 'b' = "Broseph"
charName 'c' = "Cecil" It complains that we have non-exhaustive patterns, and rightfully so. When making patterns, we should always include a catch-all pattern so that our program doesn’t crash if we get some unexpected input.
Add two different vectors:
Here’s how we would have done it if we didn’t know about pattern matching:
Well, that works, but there’s a better way to do it. Let’s modify the function so that it uses pattern matching.
Note that this is already a catch-all pattern. The type of addVectors (in both cases) is addVectors :: (Num a) => (a, a) -> (a, a) - > (a, a), so we are guaranteed to get two pairs as parameters.
For non-pairs, we can create our own way to access the components:
Note for Pattern matching lists:
Note: The x:xs pattern is used a lot, especially with recursive functions. But patterns that have : in them only match against lists of length 1 or more.
Now that we know how to pattern match against list, let’s make our own implementation of the head function.
This function is safe because it takes care of the empty list, a singleton list, a list with two elements and a list with more than two elements. Note that (x:[]) and (x:y:[]) could be rewritten as [x] and [x,y] (because its syntactic sugar, we don’t need the parentheses). We can’t rewrite (x:y:_) with square brackets because it matches any list of length 2 or more.
Note the use of parentheses for Pattern Matching and deconstructing
Note that we’ve taken care of all possible patterns of a list.

thus for
(Num b)we see that ok, its constraining the varbto a number.asince it is not explicitly mentioned, can be any type.
As Patterns
Handy way of breaking something up according to a pattern, and binding it to names while STILL keeping reference to the whole thing !
You do that by putting a name and an
@in front of a pattern.
e.g.
You can access
xsjust byxsinstead of having to dox:y:xsnormally.
e.g.
Normally we use as patterns to avoid repeating ourselves when matching against a bigger pattern when we have to use the whole thing again in the function body.
One more thing — you can’t use
++in pattern matches. If you tried to pattern match against(xs ++ ys), what would be in the first and what would be in the second list? It doesn’t make much sense. It would make sense to match stuff against(xs ++ [x,y,z])or just(xs ++ [x]), but because of the nature of lists, you can’t do that.
Last updated