We’ll continue list manipulation by considering how to filter a list.
Specifically, given a number
n and an array, we’ll return all elements of the array less than
First we declare the
type of our function, which takes a number and an array, and returns an array.
type ArrFilter = Int -> [Int] -> [Int]
f :: ArrFilter f _  =  f n (x:xs) = if x < n then x : f n xs else f n xs
f 10 [2,4,6,8,10,12,14]
List comprehensions might look a bit familiar if you’ve used them in Python before.
f2 :: ArrFilter f2 n arr = [num | num <- arr, num < n]
f2 10 [2,4,6,8,10,12,14]
f3 :: ArrFilter f3 n arr = filter (< n) arr
f3 10 [2,4,6,8,10,12,14]
Using Filter - Short
f4 :: ArrFilter f4 n = filter (< n)
f4 10 [2,4,6,8,10,12,14]
Using Filter - Shorter
f5 :: ArrFilter f5 = filter . (>)
f5 10 [2,4,6,8,10,12,14]