next up previous contents
Next: Application of associative lists Up: Associative Lists in A++ Previous: Associative Lists in A++   Contents

Abstractions for associative lists

The following examples show how a list of keys and a corresponding list of values can be combined into one associative list of pairs using the abstraction `map2', an extension of `map'.

To display an associative list using symbols as keys a new function is required, because `ldisp!' can handle only Church Numerals. We call this new function `aldisp!'.

To search for keys in the list we need another lambda abstraction, which we call `assoc'.


(define map2
  (lambda(proc l1 l2)
    (if (lor (nullp l1) 
              (nullp l2)) 
        nil
        (cons (proc (car l1) (car l2)) 
               (map2 proc (cdr l1) (cdr l2))))))


(define aldisp! (lambda (l)
                  (if (nullp l) 
                    nil
                    ((lambda()
                      (print (car (car l)))
                      (print (cdr (car  l)))
                      (aldisp! (cdr l)))))))
;
(define assoc
  (lambda(key alist)
    (if (nullp alist) 
         false
	(if (equal key (car(car alist)))  
             (car alist)
             (assoc key (cdr alist))))))

This example uses the following extensions of A++ in its original version:
next up previous contents index
Next: Application of associative lists Up: Associative Lists in A++ Previous: Associative Lists in A++   Contents   Start

Georg P. Loczewski 2004-03-05


Impressum und Datenschutz