t3x.org / sketchy / library / map.html
SketchyLISP
Reference
  Copyright (C) 2006
Nils M Holm

map

Conformance: R5RS

Purpose: Map an n-ary function f over n lists, giving a single result list. F is applied to the the list consisting of the car of each argument list, giving the first member of the result list. Map then proceeds with the cadr parts of each list, giving the next member of the result list, etc.

Arguments:
F - function to apply
A... - lists

Implementation:

(define (map f . a)
  (letrec
    ((map-list (lambda (f a r)
      (cond ((null? a) (reverse r))
        (#t (map-list f (cdr a)
                      (cons (f (car a)) r))))))
    (carof (lambda (a)
      (map-list car a '())))
    (cdrof (lambda (a)
      (map-list cdr a '())))
    (any-null? (lambda (a)
      (cond ((null? a) #f)
        ((null? (car a)) #t)
        (#t (any-null? (cdr a))))))
    (_map (lambda (a b)
      (cond ((any-null? a) (reverse b))
        (#t (_map (cdrof a)
              (cons (apply f (carof a)) b)))))))
    (cond ((null? a)
        (bottom '(too few arguments to map)))
      (#t (_map a '())))))

Example:

(map cons '(a b c) '(d e f)) 
=> ((a . d) (b . e) (c . f))

See also:
member, list?, reduce, reduce-r.