読者です 読者をやめる 読者になる 読者になる

10分なら

寝る前にやってみようと思ったのに1時間かかった件 o...rz
10分でコーディング x 2 〜リストの破壊的操作篇〜 - 'T - cadr group

最初(loop for 〜 on 〜 …)使って書き始めてはまった。

(defun nalist-to-plist (alist)
  (do* ((result (car alist))
        (a alist cdr)
        (car (car a) (car a))
        (cdr (cdr a) (cdr a)))
       ((null a) result)
    (setf (car a) (cdr car)
          (cdr car) a
          (cdr a) (car cdr))))

(defun nplist-to-alist (plist)
  (do* ((result (cdr plist))
        (p plist)
        (cdr (cdr p) (cdr p))
        (cddr (cddr p) (cddr p)))
       ((null p) result)
    (setf (cdr p) (car cdr)
          (car cdr) p
          p (cdr cdr)
          (cdr cdr) (cdr p))))

(defun test ()
  (prog ((data (loop :repeat 100000 :collect (cons 1 2))))
     (time (nalist-to-plist data)))
  (prog ((data (loop :repeat 100000 :append (list 1 2))))
     (time (nplist-to-alist data))))
CL-USER> (print (nalist-to-plist (print (nplist-to-alist '(foo 1 bar 2 baz 3)))))

((FOO . 1) (BAR . 2) (BAZ . 3)) 
(FOO 1 BAR 2 BAZ 3) 
(FOO 1 BAR 2 BAZ 3)
CL-USER> (test)
Evaluation took:
  0.001 seconds of real time
  0.000594 seconds of total run time (0.000585 user, 0.000009 system)
  100.00% CPU
  1,410,552 processor cycles
  0 bytes consed
  
Evaluation took:
  0.001 seconds of real time
  0.000622 seconds of total run time (0.000613 user, 0.000009 system)
  100.00% CPU
  1,649,724 processor cycles
  0 bytes consed
  

明日もうちょっと綺麗な書き方考える。