next up previous contents
Next: Selecting elements from a Up: Higher Order Functions Previous: Converting all elements in   Contents


Converting the `map' function: `mapc'

This abstraction illustrates the usefulness of the abstraction `curry'. `mapc' converts the `map' function, which takes two arguments into a function expecting only one argument.

The `map' functionexpects a modifier and a list as arguments. The modifier is applied to all elements of the list. If for a certain application the modifier remains constant but the list varies from case to case, it seems to be useful to have a function that expects only the list as argument. Such a function is built by `mapc'.


(define mapc (curry map))
;
;;; Example 1:
;;;
;
(define l1 (cons one 
                  (cons two 
                         (cons three 
                                (cons four 
                                       nil)))))
;
(define malzwei (mapc  (lambda(x) 
                         (mult two x)))) 
(ldisp! (malzwei l1))
                             --> 2
                                 4
                                 6
                                 8
;
;;; Example 2:
;;;
;
(define succ* (mapc succ))
;
(define l1 (cons one 
                  (cons two 
                         (cons three 
                                (cons four 
                                       nil)))))
(ldisp! (succ* l1))
                             --> 2
                                 3
                                 4
                                 5



Georg P. Loczewski 2004-03-05


Impressum und Datenschutz