Grouping keys of an associative list


/ Published in: Scheme
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.