Save 45% for a limited time.

Get access to all courses and lessons on egghead today.

Autoplay

    Break up Expressions into Cases in PureScript using Simple Pattern Matching

    Vincent OrrVincent Orr

    Pattern matching in functional programming languages is a way to break up expressions into individual cases.

    We are going to go through how to pattern match in PureScript with simple patterns, guards, array patterns and record patterns.

    Join in by going to the online PureScript editor.

    purescriptPureScript
    0.11.7 - ^0.12
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: 00:00 We'll start off by making a function and calling it Nosense. That'll have the type definition of int -> int -> int. Now we'll type Nosense N 00and Nosense N_ = N.

    00:12 I'll quickly do a demonstration of this function so you get an idea of how it works. We'll type out Nosense, the integer 22, and 0That returns us 0Let's change numbers up a little bit, 35 and 34. That returns 35. Then we'll do 12 and 0and that returns us 0So, how's our function working? Let's look at line eight. We have N and 0and inputs. What happens, if it sees a second input is a 0then it will return us 0If doesn't have 0as its second input, then it will go on the second line, which is line nine, and whatever integers you passed it, it'll return us the first one.

    00:54 The underscore, you could think of it as a wild card. Basically, it doesn't really care what it is. In this instance, it'll ignore it and return us N, which is the first integer.

    01:04 Let's expand a little bit from this and create a new function called whoIsGreater. On the next line, whoIsGreater NM | N > M = N. On the next line, we'll do another | otherwise = M. Quickly see it in action. We'll type in whoIsGreater, which returns us 44.

    01:29 How does this work? Looking at line eight, we have an N and an M. We can imagine it's 12 and 44. Then we have this pipe, which we call a guard. You can think of this guard as a check. It's checking if N is greater than M. If that's the case, it'll return N. Otherwise, if that pattern doesn't match, then we use the keyword otherwise, it will just return us M.

    01:53 In this scenario, you can think of it as an if/else statement. Calling whoIsGreater with 12 and 44 didn't match our first pattern, because 12 isn't greater than 44. It went straight to otherwise and returns us 44. Let's change the function inputs to show you the first pattern matching. We'll change whoIsGreater 12 and 11. As you can see, 12 is obviously greater than 11, and that returns us 12.

    02:19 Now I'll demonstrate another type of pattern matching. We'll create a function called isEmpty, and that's the type definition of forall A. and array A -> Boolean. I'll give a quick explanation of what forall A is doing. It's quite simply that all the As used in this type declaration must be of the same type.

    02:37 In this declaration, we've done array A. You could say, for all As in our array, should have matching types. On the next line, we'll do isEmpty open and close square brackets, which represents empty array, = true. On the next line, isEmpty_ = false.

    02:57 Let's see this in action, isEmpty, empty array. That returns us true. Now, inside that array, we'll put a 1 and a string of a, and we'll get type error. That's going back to our forall A. We expected both value to be of the same type. In this case, and int and a string obviously don't match.

    03:17 If we change our string to another int of 2, you'll see that it'll match, and that'll return us false. This is because the pattern match didn't match on line eight, went to line nine, and returned us false.

    Discuss

    Discuss