We want a block that takes a list of numbers as input, and reports a list of just the even numbers from the input.

Once again, you already know how to write this block using the keep HOF, but we're going to forget about HOFs for the moment and write it recursively:

evens

Here there are three possible cases to consider, not just the usual base case or recursive case. There's still a base case, namely an empty input list, in which case we report an empty list. But there are two recursive cases, depending on whether or not the first item of the list is even. (There has to be a first item, if we're not in the base case.) The green Operators blocks above take the remainder on dividing the first number by 2, and see if that remainder is 0, in which case the number is even.

If the first number is even, then we want to include that number in the result. So we report that number in front of the recursive call on the rest of the numbers. If the first number isn't even, then we don't want to include it in the result, so we simply report the value reported by the recursive call.


Do the following exercises using recursion, not higher order functions.

☞ Write a block ends-e that takes a list of words as input, and reports a list of those words from the input whose last letter is e .

ends-e example

☞ Write a block numbers that takes a list of mixed words and numbers as input, and reports a list of just the numbers from the input list.

numbers example