|
|||
|
Alex Mizrahi wrote:
> in kmrcl package you can find such implementations: > > (defun alist-plist (alist) > (apply #'append (mapcar #'(lambda (x) (list (car x) (cdr x))) alist))) Racket: (flatten '((k . 2) (key . 3))) => '(k 2 key 3) |
|
|
||||
|
||||
|
|
|
|||
|
On 05/26/2012 03:01 PM, WJ wrote:
> Alex Mizrahi wrote: > >> in kmrcl package you can find such implementations: >> >> (defun alist-plist (alist) >> (apply #'append (mapcar #'(lambda (x) (list (car x) (cdr x))) alist))) > > Racket: > > (flatten '((k . 2) (key . 3))) > => '(k 2 key 3) CL-USER> (defun alist->plist (alist) (loop for (car . cdr) in alist collect car collect cdr)) ALIST-TO-PLIST CL-USER> (alist->plist '((1 . 2) (3 . 4) (5 . 6))) (1 2 3 4 5 6) CL-USER> The loop solution seems pretty clean and efficient to me. This first one is dreadfull as it conses up two cons cells for each alist element only to delete them afterward. Also it only works if the alist size is less than half the largest number of arguments to a function which means it is is not reliable is the alist has more than 500 elements or so. A flatten that works on improper trees is interesting, however. |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|