Lecture overview -- Keyboard shortcut: 'u'  Previous page: The filtering function -- Keyboard shortcut: 'p'  Next page: Reduction and zipping [Section] -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home    Lecture 4 - Page 13 : 34
Functional Programming in Scheme
Higher-order Functions
Examples of filtering

As we did for mapping, we will also here study a number of examples. As before, we arrange the examples in a table where the example expressions are shown to the left, and their values to the right.

Expression

Value

(filter 
  even?
 '(1 2 3 4 5))
(2 4)
(filter 
  (negate even?)
  '(1 2 3 4 5))
(1 3 5)
(ol 
 (map li
  (filter string?
   (list 
     1 'a "First" "Second" 3))))
  1. First
  2. Second
Same as above
<ol>
  <li>First</li> <li>Second</li>
</ol>

In the first row we filter the first five natural numbers with the even? predicate. In the second row, we filter the same list of numbers with the odd? predicate. Rather than using the name odd? we form it by calculating (negate even?) . We have seen the higher-order function negate earlier in this lecture. The third and final example illustrates the filtering of a list of atoms with the string? predicate. Only strings pass the filter, and the resulting list of strings is rendered in an ordered list by means of the mirror function of the ol HTML element.