Skip to content

Instantly share code, notes, and snippets.

@ha2ne2
Last active August 29, 2015 14:18
Show Gist options
  • Save ha2ne2/74d7bfe5692e43fb288e to your computer and use it in GitHub Desktop.
Save ha2ne2/74d7bfe5692e43fb288e to your computer and use it in GitHub Desktop.
(defmacro for (data &rest body)
(let* ((var (first data))
(start (second data))
(stop (third data))
(gstop (gensym)) )
`(do ((,var ,start (1+ ,var))
(,gstop ,stop))
((> ,var ,gstop))
,@body)))
(defun calc5 (lst)
(let* ((len (length lst))
(ary (apply #'vector lst))
(table (make-array (list len) :element-type 'bit :initial-element 0))
(swap-way '())
(count 0))
(for (i 0 (1- len))
(when (zerop (aref table i))
(do ((j (aref ary i) (aref ary j)))
((= i j))
(push (list i j) swap-way)
(incf count)
(setf (aref table j) 1))
(setf (aref table i) 1)))
(list count (reverse swap-way))))
;; (calc5 '(1 2 3 0 5 6 7 4))
;=> (6 ((0 1) (0 2) (0 3) (4 5) (4 6) (4 7)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment