yamasushi(2013/05/01 07:10:20 UTC)2つの要素をもつものについて。
(defun pair (l r)
(list l r))
(defun fst (p)
(car p))
(defun snd (p)
(cadr p))
;; pair ---- (x y)
(defmacro let-pair ( (x y) pair-arg &body body )
(with-gensym (pair)
`(let* ((,pair ,pair-arg )
(,x (car ,pair))
(,y (cadr ,pair)))
,@body
)))
(defun zip-with (op l r)
(cond
((and (listp l) (listp r))
(mapcar op l r))
((vectorp l)
(zip-with op (vector->list l) r))
((vectorp r)
(zip-with op l (vector->list r)))
(t (error "cannot zip"))
))
(defun zip (l r)
(zip-with #'pair l r))
(defun unzip (z)
(loop for item in z
collecting (fst item) into l
collecting (snd item) into r
finally (return (pair l r))))
with-gensymはpractial common lispを参照。