REVISED: Wednesday, January 24, 2024
Haskell Filter.
I. HASKELL FILTER
A. HASKELL FILTER EXAMPLES
1. Generic Haskell Filters
Given p is a predicate function which takes an a to Bool, True or False, and a list of a and returns a list of a.
-- List Comprehension Filter
filterComp :: (a -> Bool) -> [a] -> [a]
filterComp p xs = [ x | x <- xs, p x ]
-- Recursion Filter
filterRec :: (a -> Bool) -> [a] -> [a]
filterRec p [] = []
filterRec p (x:xs) | p x = x : filterRec p xs
| otherwise = filterRec p xs
Prelude> let positivesComp xs = [ x | x <- xs, x > 0]
positivesComp :: (Ord t, Num t) => [t] -> [t]
Prelude>
Prelude> positivesComp [1,-2,3,-4]
[1,3]
it :: (Ord t, Num t) => [t]
Prelude>
"Copy Paste" the following function into your text editor and "File, Save As" positivesRec.hs to your working directory:
| otherwise = positivesRec xs
Load the above function into GHCi as follows:
Prelude> :load positivesRec
[1 of 1] Compiling Main ( positivesRec.hs, interpreted )
Ok, modules loaded: Main.
Prelude
From GHCi call the function as shown below:
Prelude> positivesRec [1,-2,3,-4]
[1,3]
it :: (Ord t, Num t) => [t]
Prelude>
The following function is short; therefore, you can either "Copy Paste" the following function into your text editor and "File, Save As" digitsComp.hs to your working directory:
digitsComp :: [Char] -> [Char]
digitsComp xs = [ x | x <- xs, isDigit x ]
Or load the above function into GHCi as follows:
Prelude> import Data.Char
Prelude>
Prelude> let digitsComp xs = [ x | x <- xs, isDigit x ]
it :: [Char]
Prelude>
"Copy Paste" the following function into your text editor and "File, Save As" digitsRec.hs to your working directory:
digitsRec :: [Char] -> [Char]
digitsRec [] = []
digitsRec (x:xs) | isDigit x = x : digitsRec xs
| otherwise = digitsRec xs
Load the above function into GHCi as follows:
Prelude> :load digitsRec
[1 of 1] Compiling Main ( digitsRec.hs, interpreted )
Ok, modules loaded: Main.
Prelude>
Prelude> digitsRec "a1b2c3d4"
"1234"
it :: [Char]
Prelude>
B. ABSTRACTION PRINCIPLE EXAMPLE
"Copy Paste" the following function into your text editor and "File, Save As" myFilter.hs to your working directory:
myFilter :: (a -> Bool) -> [a] -> [a]
myFilter _ [] = []
myFilter p (x:xs)
| p x = x : myFilter p xs
| otherwise = myFilter p xs
Load the above function into GHCi as follows:
Prelude> :load myFilter
[1 of 1] Compiling Main ( myFilter.hs, interpreted )
Ok, modules loaded: Main.
Prelude>
From GHCi call the function as follows:
Prelude> myFilter (< 4) [1,2,3,4,5,6,7,8,9,10]
[1,2,3]
Prelude>
Prelude> myFilter odd [1,2,3,4,5,6,7,8,9,10]
[1,3,5,7,9]
Prelude>
Prelude> myFilter even [1,2,3,4,5,6,7,8,9,10]
[2,4,6,8,10]
Prelude>
Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.
Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.
Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.
-- List Comprehension Filter
filterComp :: (a -> Bool) -> [a] -> [a]
filterComp p xs = [ x | x <- xs, p x ]
-- Recursion Filter
filterRec :: (a -> Bool) -> [a] -> [a]
filterRec p [] = []
filterRec p (x:xs) | p x = x : filterRec p xs
| otherwise = filterRec p xs
2. Positive Haskell Filters
-- List Comprehension Positives
positivesComp :: (Num t, Ord t) => [t] -> [t]
positivesComp xs = [ x | x <- xs, x > 0]
Prelude> let positivesComp xs = [ x | x <- xs, x > 0]
positivesComp :: (Ord t, Num t) => [t] -> [t]
Prelude>
Prelude> positivesComp [1,-2,3,-4]
[1,3]
it :: (Ord t, Num t) => [t]
Prelude>
"Copy Paste" the following function into your text editor and "File, Save As" positivesRec.hs to your working directory:
-- Recursion Positives
positivesRec :: (Num t, Ord t) => [t] -> [t]
positivesRec [] = []
positivesRec (x:xs) | x > 0 = x : positivesRec xs| otherwise = positivesRec xs
Load the above function into GHCi as follows:
Prelude> :load positivesRec
[1 of 1] Compiling Main ( positivesRec.hs, interpreted )
Ok, modules loaded: Main.
Prelude
From GHCi call the function as shown below:
Prelude> positivesRec [1,-2,3,-4]
[1,3]
it :: (Ord t, Num t) => [t]
Prelude>
3. Haskell isDigit Filters
The following function is short; therefore, you can either "Copy Paste" the following function into your text editor and "File, Save As" digitsComp.hs to your working directory:
-- List Comprehension isDigit
import Data.ChardigitsComp :: [Char] -> [Char]
digitsComp xs = [ x | x <- xs, isDigit x ]
Or load the above function into GHCi as follows:
Prelude> import Data.Char
Prelude>
Prelude> let digitsComp xs = [ x | x <- xs, isDigit x ]
digitsComp :: [Char] -> [Char]
Prelude>
Either way, from GHCi call the function as shown below:
Prelude> digitsComp "a1b2c3d4"
"1234"Prelude>
Either way, from GHCi call the function as shown below:
Prelude> digitsComp "a1b2c3d4"
it :: [Char]
Prelude>
"Copy Paste" the following function into your text editor and "File, Save As" digitsRec.hs to your working directory:
-- Recursion isDigit
import Data.ChardigitsRec :: [Char] -> [Char]
digitsRec [] = []
digitsRec (x:xs) | isDigit x = x : digitsRec xs
| otherwise = digitsRec xs
Load the above function into GHCi as follows:
Prelude> :load digitsRec
[1 of 1] Compiling Main ( digitsRec.hs, interpreted )
Ok, modules loaded: Main.
Prelude>
Prelude> digitsRec "a1b2c3d4"
"1234"
it :: [Char]
Prelude>
B. ABSTRACTION PRINCIPLE EXAMPLE
"Copy Paste" the following function into your text editor and "File, Save As" myFilter.hs to your working directory:
myFilter :: (a -> Bool) -> [a] -> [a]
myFilter _ [] = []
myFilter p (x:xs)
| p x = x : myFilter p xs
| otherwise = myFilter p xs
Load the above function into GHCi as follows:
Prelude> :load myFilter
[1 of 1] Compiling Main ( myFilter.hs, interpreted )
Ok, modules loaded: Main.
Prelude>
From GHCi call the function as follows:
Prelude> myFilter (< 4) [1,2,3,4,5,6,7,8,9,10]
[1,2,3]
Prelude>
Prelude> myFilter odd [1,2,3,4,5,6,7,8,9,10]
[1,3,5,7,9]
Prelude>
Prelude> myFilter even [1,2,3,4,5,6,7,8,9,10]
[2,4,6,8,10]
Prelude>
II. REFERENCES
Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.
Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.
Goerzen, J. & O'Sullivan, B. & Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.
Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.
Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.
Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.
In this tutorial, you have been introduced to the Haskell filter.