2

I am trying to check if a word is an element of a list. It it isn't then add the word to the front of the list, but if it is move that word to the front of the list. I am able to add the word to the list if it isn't already there, but I don't know how to the move the element to the front of the list if it is in the list already. Here is my code:

(defun movetofront (word lst)
  (cond
    ((member word lst) 
     (remove word lst))
    (T (cons word lst)))) 
Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
ObiJuanKanobe
  • 45
  • 1
  • 6

1 Answers1

6

You do not have to check for the presence:

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

Note that if word is, e.g., a string, you will need to pass :test to remove.

Community
  • 1
  • 1
sds
  • 58,617
  • 29
  • 161
  • 278