Updated April 20, 2023
Definition of Haskell Filter Function
Haskell is a function programming language, and filter is a function to filter any data structure in Haskell. This function takes data structure that can be anything and try to result out the new data structure which contain only the filter value for which a predicate return true, false values should not be included in the new data structure because we have applied a condition to filter among these present element. This functions will always return us the new data structure containing the result. In short, it is a high-order function used to filter data structure element based on some predicate passed. In the coming section, we will discuss the internal working and implementation of filter function in detail for its usage while programming.
Syntax:
As we already discussed that it uses a predicate to filter the element of the data structure, and return us the new data structure. Let’s take a look at its syntax for better understanding for beginners see below;
filter :: (a -> Bool) -> [a] -> [a]
As you can see in the above lines of code this is the syntax given by the official documentation of Haskell for filter function on their website. Here we are using predicate with list or data structure to filter out the element based on the predicate. Let’s take a practice syntax for beginners for more clarity see below;
e.g. :
filter (your_predicate) your_list
As this syntax is pretty much clear from the above one, so in the coming section, we will discuss more the internal working and how to use filter function in a program in detail.
How filter function works in Haskell?
As we know a filter is used to filter or extract the data or elements from the given data structure for use. This filter condition can be anything based on the predicate we apply. Once we apply the predicate it will return us the elements which satisfy the predicate condition. Let’s take a look at its flow diagram how it internally works in Haskell to filter data see below;
Steps are follow;
1) First we use the filter function to filter out the data structure.
2) Here we use predicate with the list or data structure.
3) If the condition satisfies then the predicate will return us True, if the predicate condition does not match will return us False.
4) Filter function will always return us new list; array or data structure we process.
5) return type for predicate is Boolean.
6) Return type for filter function is newly created data structure.
7) If we want to use filter function then we have to use two things together, one is predicate and other one is data to be processed.
Now take one sample example to understand how it works programmatically see below;
e.g. :
mylist = [21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
filter even mylist
In the above lines of code, we are using filter function from Haskell, one more important things here is it is an in-built function in Haskell. We do not require to import any external library for this. So in the first step, we are creating list inside these we have some values which are either odd or even. After this, we are calling filter function and hear we are using predicate ‘even’ to filter out only even values from the processed list. Immediately after this predicate, we are passing our list variable that we have created. So this will start processing the elements of the list one by one, of the element, is even the predicate will return true else it will return false. All the elements for which the predicate return true will be return as the output, and store in the newly created list. This is how it works programmatically in Haskell and easy to use as well.
Examples
In this example, we are trying to use filter function with the ‘odd’ predicate. So it will return us only the odd values from the given list. This is a sample example for beginners to start using filter while programming.
Example #1
Code:
main = do
putStrLn "Demo to show filter function in Haskell !!"
putStrLn "filtering out only odd values form the list "
putStrLn "calling printing result "
let list1= [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
print(filter odd list1 )
let list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(filter odd list2 )
Output:
In this example, we are trying to use filter function with the even predicate. So it will return us only the even values from the given list. This is a sample example for beginners to start using filter while programming.
Example #2
Code:
main = do
putStrLn "Demo to show filter function in Haskell !!"
putStrLn "filtering out only even values form the list "
putStrLn "calling printing result "
let list1= [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
print(filter even list1 )
let list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(filter even list2 )
Output:
Example #3
In this, we have applied custom predicate to filter out the elements we have used greater than symbol for this as predicate.
Code:
main = do
putStrLn "Demo to show filter function in Haskell !!"
putStrLn "calling printing result "
let list1= [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
print(filter (>15) list1 )
let list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(filter (>6) list2 )
Output:
Conclusion
By using filter functionality in Haskell or any programming language we can filter out a large number of data structure based on the condition and process them for future use as per the requirement. As we have seen already it is very easy to use and handle, without heavy code structure.
Recommended Articles
We hope that this EDUCBA information on “Haskell Filter Function” was beneficial to you. You can view EDUCBA’s recommended articles for more information.