排序代码,基本上是我从这个翻译过来的翻译:
insertion(A)
for i from 2 to n
j=i-1
while (j>=1) AND (A[j]>A[j+1])do
t=A[j+1]
A[j+1]=A[j]
A[j]=t
j=j-1
我对lisp的翻译是
(defun insertion (unsorted-vector)
(let ((vector (copy-seq unsorted-vector))
(n (length unsorted-vector)))
(loop :for i : from 2: below (n)
:do ((j (- i 1))
(loop :do (AND (>= (j 1))
(> (aref vector j) (aref vector (+ j 1))))
(rotatef (aref vector j) (aref vector (+ j 1)))
(j (- j 1))))
vector)))
(insertion #(5 1 3 2))
但它给我一条消息说:阅读时出错:#\ Space冒号后是非法字符。我不明白这是什么意思,也不知道代码中是否还有其他错误。
答案 0 :(得分:2)
因为有空间。看看你的代码,你应该写:from
或from
,而不是: from
,below
也是如此。
(defun insertion (unsorted-vector)
(let ((vector (copy-seq unsorted-vector))
(n (length unsorted-vector)))
(loop :for i : from 2: below (n) ;; your problem
;;; ^^^ ^^^ ;; <= is here
;;; Change to :from and :below
:do ((j (- i 1))
(loop :do (AND (>= (j 1))
(> (aref vector j) (aref vector (+ j 1))))
(rotatef (aref vector j) (aref vector (+ j 1)))
(j (- j 1))))
vector)))