Skip to content
This repository has been archived by the owner on Dec 21, 2022. It is now read-only.

alist2plist #111

Open
reiniervandijk opened this issue May 8, 2013 · 2 comments
Open

alist2plist #111

reiniervandijk opened this issue May 8, 2013 · 2 comments

Comments

@reiniervandijk
Copy link

gdl:alist2plist function gives error when official alist is provided. "official" according to the definition: an a-list is a list of pairs (conses):

http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node153.html

Looking at the source, gdl:alist2plist can only deal with list pairs, and not dotted pairs, although these are perfectly valid building stones of a alist.

Replicate

(gdl:alist2plist '(("foo" . 3) ("bar" . 4)))
@genworks genworks added the $ label Jan 30, 2015
@genworks
Copy link
Owner

Open for submission: $50

@reiniervandijk
Copy link
Author

(in-package :gdl-user)

(defun alist2plist (alist)
  "Plist. Converts an assoc-list to a plist.
:arguments (alist \"Assoc-List\"). Supports both alists
with list syntax or dotted style notation. Examples:

gdl-user> (alist2plist '((:foo . 1) (:bar . 2)))
(:foo 1 :bar 2)

gdl-user> (alist2plist '((:foo 1) (:bar 2)))
(:foo 1 :bar 2)
"

  (when alist
    (cons (first (first alist))
          (cons
       ;; test cdr list syntax (1 2)
       ;; or dotted pair notation (1 . 2)
       (let ((token (rest (first alist))))
         (if (consp token)
         ;; list syntax
         (first token)
         ;; dotted pair
         token))
       ;; recurse
           (alist2plist (rest alist))))))

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants