Haskell - Filter Positions In List
Github: FilterPositionsInList.hs We will now manipulate Lists by filtering through and returning only the elements that occur in the odd positions.
We declare our function type which takes in a List as an argument and returns a List.
type FilterPos = [Int] -> [Int]
Using List Comprehension
f :: FilterPos
f lst = [lst!!n | n<-[0..length lst - 1], odd n]
f [2,4,6,8,10,12,14]
[4,8,12]
Using Recursion
f2 :: FilterPos
f2 (_:x:xs) = x : f2 xs
f2 _ = []
f2 [2,4,6,8,10,12,14]
[4,8,12]
Using Higher Order Functions (map
, filter
, zip
)
f3 :: FilterPos
f3 = map snd . filter (odd . fst) . zip [0..]
f3 [2,4,6,8,10,12,14]
[4,8,12]