lispcommon-lisp

Get numbers for the lottery


As part of learning Lisp I'm currently trying to write a function that helps me fill out my lottery ticket. I want that function to return

So far, I'm done with four of the five requirements. This is my current code:

(defun lottery ()
  (sort (loop repeat 6 collect (1+ (random 49))) #'<))

When I run this function I get something such as:

(lottery)
;; => (3 10 23 29 41 43)

Basically, everything's fine - except that sometimes I have the very same number twice within the list. And here it starts to get puzzling. My problem is that I'm not too sure on how to solve this in a Lisp way. There are multiple options I can think of:

What do you think of those options, and are there other ways of implementing this? How would an advanced Lisp developer solve this?

Any hints?


Solution

  • Create a list of all the numbers from 1 to 49, shuffle, take 6, sort.

    => (sort (take 6 (shuffle (range 1 50))))
    ; (8 14 16 23 34 39)
    

    Addition by original poster:

    Just to show the final implementation, I'm adding it here:

    (defun shuffle (list)
      (let ((len (length list)))
        (loop repeat len
          do
            (rotatef
              (nth (random len) list)
              (nth (random len) list))
          finally
            (return list))))
    
    (defun lottery ()
      (sort (subseq (shuffle (loop for i from 1 to 49 collect i)) 0 6) #'<))
    
    (lottery)
    ;; => (5 6 17 21 35 37)