如何将列表中的元素移动到 LISP 中列表的前面

How to move an element in a list to the front of the list in LISP

我正在尝试检查某个词是否是列表的元素。它不是然后将单词添加到列表的前面,但如果它是将该单词移动到列表的前面。如果单词不存在,我可以将其添加到列表中,但如果元素已经在列表中,我不知道如何将该元素移动到列表的前面。这是我的代码:

(defun movetofront (word lst)
  (cond
    ((member word lst) 
     (remove word lst))
    (T (cons word lst)))) 

您不必检查是否存在:

(defun move-to-front (word list)
   (cons word (remove word list)))

请注意,如果 wordstringyou will need to pass :test to remove