next up previous contents index
Next: Converting the `map' function: Up: Higher Order Functions Previous: Changing the arity of   Contents   Index


Converting all elements in a list: `map'

The abstraction `map' is used to modify all elements of a list according to a mapping function supplied as its first argument.

The function achieves its purpose, like all other functions presented in this chapter, not by modifying the original list but by creating a new one having the desired characteristic.

Remark: This is a trademark of functional programming, providing a higher robustness compared to programs causing side effects, i.e. programs that destructively modify data. Functional programs are also easier to verify and eventually to debug than so called imperative programs.

A++ supports all three major programming paradigms but in this chapter the functional programming style is exclusively applied and generally preferred to the others. Banning the other programming styles generally would not be appropriate, because the object oriented paradigm is not only popular but also very useful and the imperative programming style cannot not be avoided entirely.


\begin{lstlisting}[language=Scm]{}
(define map (lambda(f l)
(if (nullp l)
nil
(cons (f (car l)) (map f (cdr l))))))
\end{lstlisting}


\begin{lstlisting}[language=Scm]{}
(define l1 (cons one
(cons two
(cons thre...
...
(ldisp! (map (lambda(x)
(mult two x))
l1))
--> 2
4
6
8
\end{lstlisting}

next up previous contents index
Next: Converting the `map' function: Up: Higher Order Functions Previous: Changing the arity of   Contents   Index
domain access counter Georg P. Loczewski 2004-03-05