In Ruby Programming language myList.shuffle.first
is slower than myList.sample
since it completely shuffle the list and pick the first element. If something similar (shuffle and take first) is done in Haskell, will that be as fast as the later (sampling the array)? I am assuming that the list will be shuffled lazily so picking the first element or taking the sample will be virtually same.
Asked
Active
Viewed 195 times
2

Numeri
- 1,027
- 4
- 14
- 32

Kavin Eswaramoorthy
- 1,595
- 11
- 19
-
6It depends. AFAIK the Haskell standard doesn't prescribe any specific shuffle or sampling algorithm, so it completely depends on the implementation. In any case lazyness does **not** mean that the computer will do as few computations as possible to produce the output. It simply means that it avoids "obviously useless computations" for the given output. In other words `head $ shuffle something` may well do some operation used to shuffle elements other than the first, depending on the implementation. Lazyness simply tells you that when the first element is produced other steps are avoided. – Bakuriu Oct 19 '15 at 13:37
-
maybe you can have a look and tell us [which `shuffle`](hayoo.fh-wedel.de/?query=shuffle) you want to use ;) – Random Dev Oct 19 '15 at 13:51
-
2It can be written to behave that way, using the decorate-sort-undecorate pattern: first label each element in your list with a random number; sort by the labels; throw away the labels; and take the first element. The standard implementation of `sort` will make this an O(n) operation, just like sampling would be. Not sure if there are any packages that offer this out of the box, and of course the manually written version of the algorithm may have better constants. – Daniel Wagner Oct 19 '15 at 17:08
-
@DanielWagner you should make that comment an answer :-) – sclv Mar 20 '16 at 06:53
-
@DanielWagner: I've added your answer as Community Wiki – Cactus Mar 29 '16 at 08:27
1 Answers
1
It can be written to behave that way, using the decorate-sort-undecorate pattern: first label each element in your list with a random number; sort by the labels; throw away the labels; and take the first element. The standard implementation of sort
will make this an O(n) operation, just like sampling would be. Not sure if there are any packages that offer this out of the box, and of course the manually written version of the algorithm may have better constants.

Cactus
- 27,075
- 9
- 69
- 149