vendredi 1 mai 2020

Trying to return unique elements from list

I am really new to Lisp programming thus I would prefer figuring it on my own so any tips are appreciated! My goal is to retrieve a list of randomly picked unique items. The below code can sometimes return a list such as ("has a ball" "has a ball"). I would prefer it to check whether the members of the list are unique and if not discard one of the same items and add a random unique member.

   (defvar *pirateAttributes* 
    (list "has a pirate costume" "has a wooden leg" "has a 
    ball")
   )

   (defun select-attributes (n)
     (loop repeat n
        collect (nth (random (length *pirateAttributes*)) 
        *pirateAttributes*)
     ) 
   )

  (select-attributes 2)



Aucun commentaire:

Enregistrer un commentaire