Posted By

jarnaldich on 04/30/08


Tagged

list alist


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

magicrebirth


Grouping keys of an associative list


 / Published in: Scheme
 

Given an an associative list ((k1 . v1) (k2 . v2) ... (kn . vn)), with possibly equal values for ki, it returns another alist where all cells have distinct keys and where values originally associated to the same key have been grouped as a list in the new cons cell. Ouch! This is much harder to explain than to code. Just check the commented example in the code.

  1. ;; it requires srfi-1 (lists), for fold-right.
  2. (define (alist-group-keys alist)
  3. (let ((create-cons-cell-or-append-to-existing
  4. (lambda (current acum)
  5. (let* ((key (car current))
  6. (value (cdr current))
  7. (cell (assoc key acum)))
  8. (if cell ; Key already seen, append current value to the list
  9. (begin
  10. (set-cdr! cell (cons value (cdr cell)))
  11. acum)
  12. ;else
  13. (cons (list key value) acum))))))
  14. (fold-right create-cons-cell-or-append-to-existing '() alist)))
  15.  
  16. ;; Example. Given an alist of authors and books:
  17. ;
  18. ; (alist-group-keys '(("Roth, Henry" . "Call It Sleep")
  19. ; ("Roth, Henry" . "Mercy of a Rude Stream")
  20. ; ("Houllebecq, Michel" . "Extension du domaine de la lutte")
  21. ; ("Houllebecq, Michel" . "Plateforme")))
  22. ;
  23. ;; It would return the books grouped by author:
  24. ;
  25. ;(("Roth, Henry" "Call It Sleep" "Mercy of a Rude Stream")
  26. ; ("Houllebecq, Michel" "Extension du domaine de la lutte" "Plateforme"))
  27. ;

Report this snippet  

You need to login to post a comment.