Last active
December 8, 2022 03:39
-
-
Save gwerbin/edbf0c865fd9f02f2372fe2a100824ba to your computer and use it in GitHub Desktop.
Collect a list into a list of pairs (an "alist")
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; https://stackoverflow.com/a/40028542/2954547 | |
(defun collect-alist-loop (items) | |
(unless (evenp (length items)) | |
(error "Items must have an even number of pairs!")) | |
(loop | |
:for (a b) | |
:on items | |
:by #'cddr | |
:collect (cons a b))) | |
(defun collect-alist-mut (items) | |
(unless (evenp (length items)) | |
(error "Items must have an even number of pairs!")) | |
(do ((accum '())) | |
((null items) | |
(nreverse accum)) | |
(let ((key (pop items)) | |
(val (pop items))) | |
(push (cons key val) accum)))) | |
(defun collect-alist-rec (items) | |
(unless (evenp (length items)) | |
(error "Items must have an even number of pairs!")) | |
(labels ((recur (&optional (items items) (accum '())) | |
(if (null items) | |
(nreverse accum) | |
(let* ((key (car items)) | |
(val (cadr items)) | |
(pair (cons key val))) | |
(recur (cddr items) (cons pair accum)))))) | |
(recur))) | |
(format t "~A~%" (collect-alist-loop '(a b c d e f))) | |
(format t "~A~%" (collect-alist-mut '(a b c d e f))) | |
(format t "~A~%" (collect-alist-rec '(a b c d e f))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (collect-alist-rec items) | |
(unless (even? (length items)) | |
(error "Items must have an even number of pairs!")) | |
(let loop ((items items) | |
(accum '())) | |
(if (null? items) | |
(reverse accum) | |
(let* ((key (car items)) | |
(val (cadr items)) | |
(pair (cons key val))) | |
(loop (cddr items) (cons pair accum)))))) | |
(print (collect-alist-rec '(a b c d e f))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment