Updated April 19, 2023
Introduction to Haskell pattern matching
pattern matching is stands as its name suggest, we try to match a value against any given pattern of the value machine then we can say it is success. In Haskell pattern matching does the same thing, which they or attempt to match any given value or user passed value to the function, after this we can return any value we want or simply we can assign the value to the variable and return it. Pattern matching is very useful and it can be applied on anything like, numbers, character, string, tuple and list etc in Haskell. In the coming section of the tutorial we will see the detail working of pattern matching also the implementation of pattern matching in Haskell, for better understanding and start using in program for beginners.
As discussed pattern matching is used to match given value to and return result accordingly. Let’s take an example where we can have implemented the pattern matching in Haskell, also first we will see the syntax for better understanding for beginners see below;
name of your pattern = corresponding value to be executed.
.. any number of cases or pattern we can have
As you can see in the above lines of syntax we are creating a pattern first we need to give the name of the pattern and after this value to be matching, followed by the value that we want to return. Let’s take a practice syntax for beginners to understand it better;
demo val = "success"
As you can see in the above line of code, it is much clear now to implemented but in the coming section we will see have more deep looking inside the internal working of this, for beginners to understand this and start using it.
How to perform pattern matching in Haskell?
As now we already know that pattern matching is used to match a value against a particular pattern. In Haskell we can match any type such as number, string, character, list tuple etc. Also its syntax is pretty much clear and easy to implement as well. In this section first we will look how it works internally and have some sample to understand its working in detail. Let’s get started see below;
1) pattern matching: We can match any type of value using the pattern matching in Haskell, also we can have multiple pattern passed which turn return the different value for the variable. This pattern matching somewhat works in the same way like cases we have in different language for instance, inside the case we can have pattern and after matching of the pattern we can return the value we want. They will calculate the pattern from top to bottom order, suppose if we have 10 pattern it will execute from top and bottom. Let’s take one sample piece of code to understand it better see below;
dempfun :: (Integral a) => a -> String dempfun 10 = "i am ten!" dempfun 20 = "i am twenty!" dempfun 30 = "i am thirty!" dempfun 40 = "i am fourty!" dempfun 50 = "i am fifty!" dempfun 60 = "i am sixty!" dempfun 70 = "i am seventy!" dempfun x = "not matching anything here !!"
As you can see in the above lines of example we are trying to calculate an integer which should be between 10 – 70, if the value less or greater than the mentioned numbers then we will put those value to the catch all block of pattern matching. Here the code will start executing from the top and precede to bottom one by one. If the value passed doesn’t match it will return catch all statement string. Also for instance we pass ’30’ as the value which needs to be check against the pattern cocreated then as a result it will return as ‘i am thirty!’ as the string. And let’s suppose if i try to pass 1000 as the value to match against the pattern created then it will return us ”not matching anything here!!” as the output. So always try to keep the catch all block to avoid error or any unwanted event to occur.
2) catch all block: If we do not provide the catch all block then it will throw as error, because while doing pattern matching it will not able to find anything and throw us error. If you provide the catch block then the error can be avoided also, it is safer to use also known as default pattern matching block in Haskell.
Points to remember while working with pattern matching in Haskell are as follow;
1. We should use the catch block as the default pattern when nothing matches/
2. It is an in-build feature, we do not require to add or install external dependency for this.
3. We can write as many patterns for a function we want there is no restriction for it.
4. Always remember the pattern will execute from top to bottom.
1) In this example we are trying to matching the values passed as integer and it should be between 10 to 70, if the value does not match it will print the catch-all block for us, this is a sample example for beginners to start with pattern matching in Haskell.
Note: also we do not require any library to use to implement this in our program.
dempfun :: (Integral a) => a -> String dempfun 10 = "i am ten!" dempfun 20 = "i am twenty!" dempfun 30 = "i am thirty!" dempfun 40 = "i am fourty!" dempfun 50 = "i am fifty!" dempfun 60 = "i am sixty!" dempfun 70 = "i am seventy!" dempfun x = "not matching anything here !!" main = do print("Demo to show not equal operator in Haskell !!") let result1 = dempfun 10 let result2 = dempfun 70 let result3 = dempfun 50 let result4 = dempfun 30 let result5 = dempfun 90 let result6 = dempfun 20 let result7 = dempfun 100 print("print result after calling the function for pattern matching !!!") print("result one is ", result1) print("result two is ", result2) print("result three is ", result3) print("result four is ", result4) print("result five is ", result5) print("result six is ", result6) print("result seven is ", result7)
By the use of pattern matching we can easily find the matching value inside the list, tuple, number or string etc. Also the syntax for pattern matching is easy to use and implement in Haskell. It works in the same way like any other programming languge, where some values are used to match against the pattern and get the desired result.
We hope that this EDUCBA information on “Haskell pattern matching” was beneficial to you. You can view EDUCBA’s recommended articles for more information.