Idiomatic Lisp way to create a list of sorted random numbers?

I would like to know what is the common way to create a sorted list of random numbers in Common Lisp. In Clojure, this is pretty simple:

(sort (take 10 (repeatedly #(rand 10))))

I found that the following works in CL:

(sort (loop for n below 10 collect (random 10)) #'<)

but not readable. Is there a cleaner way to express the same thing?

+4
source share
2 answers

Nearly:

(sort (loop repeat 10 collect (random 10)) #'<)
+7
source

, sds answer - , map-in, , , ( ). , ; , .

(sort (map-into (make-list 10) #'(lambda () (random 10))) '<)
;=> (0 2 2 2 4 5 6 6 8 9)
(let ((l (make-list 10)))
  (sort (map-into l #'(lambda () (random 10))) '<))
;=> (1 1 3 3 4 6 7 8 8 9)
+2

Source: https://habr.com/ru/post/1610662/


All Articles