Created
January 18, 2015 02:11
-
-
Save TheSeamau5/07553149ba3d499e6436 to your computer and use it in GitHub Desktop.
Function to shuffle a list (useful to shuffle cards for example)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import List (..) | |
import Random (..) | |
import Text (asText) | |
shuffle : List a -> List a | |
shuffle lt = | |
let len = length lt | |
fgen = float 0 1 | |
lgen = list len fgen | |
rlist = fst <| generate lgen (initialSeed 31415) | |
total = sum rlist | |
nlist = map (\x -> x / total) rlist | |
zip l1 l2 = | |
case l1 of | |
[] -> [] | |
x :: xs -> | |
case l2 of | |
[] -> [] | |
y :: ys -> (x,y) :: zip xs ys | |
tlist = zip lt nlist | |
flist = sortBy snd tlist | |
in fst <| unzip flist | |
test = ["a", "b", "c", "d", "e", "f"] | |
stest = shuffle test | |
main = asText stest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think it would be beneficial to not
import List (...)
, because it's really hard to distinguish between functions and variables. Thanks, though!