Pattern Matching in PureScript
Before, we just covered a little bit of pattern matching in functions - let’s go into more detail, and learn about pattern matching for different data types.
Array pattern matching
Section titled “Array pattern matching”One of the benefits lists have compared to arrays is their pattern matching. With arrays, we have the problem that we can only pattern matchem them against arrays with fixed length. Let’s look at an example.
arraySum :: Array Int -> IntarraySum [x, y] = x + yarraySum [x] = xarraySum _ = 0As you can see, the defined size of the arrays is fixed as we provide the exact structure. We can pass [], [x], or [x, y] to the function, but not an array with 3 or more fields. Thankfully, lists offer more flexibility when it comes to cases like this
List pattern matching
Section titled “List pattern matching”Disclaimer: x is used for the head of a list, and xs for the tail of it. Therefore, the default “head” function could look like this:
listHead :: forall a. List a -> Maybe alistHead Nil = NothinglistHead (x : _) = Just xAs x and xs represent head and tail, we can pattern-match like this no matter the size of the list.
Record pattern matching
Section titled “Record pattern matching”Records are like objects in JavaScript or dictionaries in Python - I covered them in more detail here.
Let’s create a record type called Bankaccount, that holds some basic information:
type Bankaccount = { owner :: String, balance :: Int }Now, let’s create a function that doubles the balance for whatever reason:
doubleBalance :: Bankaccount -> IntdoubleBalance { balance } = 2 * balanceAs you can see, we can pattern-match with an exact type of field in the record type.