Exercise: Count Even Integers in a List
Use pattern matching to count the number of even integers in a list.
We'll cover the following
Problem Statement
You must implement the countEven()
function which counts the number of even integers in a list. Since a list can’t be iterated like an array, you must use pattern matching and recursion to solve this problem.
Sample Input
list = [10, 3, 17, 23, 40, 22, 99]
Sample Output
3
Coding Challenge
Think carefully about the logic behind this exercise before moving on to the actual implementation. We already know that a list can be destructured into its head
and ...tail
. Use this to create the required function.
If you feel stuck, you can always refer to the solution review in the next lesson.
Good luck!
Get hands-on with 1400+ tech skills courses.