Get numbers for the lottery

前端 未结 3 1743
悲&欢浪女
悲&欢浪女 2021-01-15 01:45

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

  • a list
  • of
3条回答
  •  心在旅途
    2021-01-15 02:44

    Using an accumulator fed by recursion:

    (defun lottery ()
      (setf grid (loop for i from 1 to 49 collect i))
      (labels ((choose (source num)
        (if (or (null source) (zerop num))
          nil
          (let ((elem (nth (random (length source)) source)))
            (cons elem (choose (remove elem source) (1- num)))))))
        (sort (choose grid 6) #'<)))
    

    The lottery function is first generating a list of numbers put into the grid variable. Then we define an internal choose local function. This function gets one random number in the list it is provided inside the interface and calls itself on the list minus the chosen element. The last step is to sort the resulting list.

提交回复
热议问题