Étude 7-6: Explaining an Algorithm
You need a way to shuffle the deck of cards. This is the code for doing a shuffle, taken from the Literate Programs Wiki.
shuffle
(
List
)
->
shuffle
(
List
,
[]).
shuffle
([],
Acc
)
->
Acc
;
shuffle
(
List
,
Acc
)
->
{
Leading
,
[
H
|
T
]}
=
lists
:
split
(
random
:
uniform
(
length
(
List
))
-
1
,
List
),
shuffle
(
Leading
++
T
,
[
H
|
Acc
]).
Wait a moment. If I’ve just given you the code, what’s the purpose
of this étude? I want you to understand the code. The object of this
étude is to write the documentation for the algorithm.
If you aren’t sure what the code does, try adding some
io:format
statements to see what is happening. If you’re totally
stuck, see the explanation from Literate Programs site.
Get Études for Erlang now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.