Lecture overview -- Keyboard shortcut: 'u'  Previous page: The mapping function -- Keyboard shortcut: 'p'  Next page: Filtering -- 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 10 : 34
Functional Programming in Scheme
Higher-order Functions
Examples of mapping

We will now study a number of examples.

Expresion

Value

(map 
  string?
  (list 1 'en "en" 2 'to "to"))
(#f #f #t #f #f #t)
(map 
   (lambda (x) (* 2 x))
   (list 10 20 30 40))
(20 40 60 80)
(ul
  (map 
   (compose li 
    (compose b 
     (lambda (x)
      (font-color red x))))
   (list "a" "b" "c")
  )
)
  • a
  • b
  • c
Same as above
<ul>
  <li>
   <b><font color = "#ff0000">a</font></b>
  </li>
  <li>
   <b><font color = "#ff0000">b</font></b>
  </li>
   <li>
    <b><font color = "#ff0000">c</font></b>
  </li>
</ul>

In the first row we map the string? predicate on a list of atoms (number, symbols, and strings). This reveals (in terms of boolean values) which of the elements that are strings. In the second row of the table, we map a 'multiply with 2' function on a list of numbers. The third row is more interesting. Here we map the composition of li , b , and red font coloring on the elements a, b, and c. When passed to the HTML mirror function ul , this makes an unordered list with red and bold items. Notice that the compose function used in the example is a higher-order function that can compose two or more functions. The function compose from lib/general.scm is such a function. Notice also that the HTML mirror function ul receives a list, not a string. The fifth and final row illustrates the raw HTML output, instead of the nicer rendering of the unordered list, which we used in the third row.