Reputation: 197
I am wondering if in Racket I could get n number of items from a list I've already created. So lets say I made a list in Racket
(define base(list 1 2 3 4 5 6 7 8 9 10))
Now I want to define a function which will pick n number of items from this list and display them in a new list. So lets say n=4 I would want 4 random items from the base list I've made above. An example of the output im looking for would be
'(9 4 3 10)
Is there a way I can do this in Racket?
Upvotes: 1
Views: 324
Reputation: 236122
There are built-in procedures that literally do what you need: shuffling the list and taking n elements from it. Try this:
(define (take-n-random lst n)
(take (shuffle lst) n))
(define base (list 1 2 3 4 5 6 7 8 9 10))
(take-n-random base 4)
=> '(6 9 1 7)
Upvotes: 1