Skip to content

Instantly share code, notes, and snippets.

@TheSeamau5
Created January 18, 2015 02:11
Show Gist options
  • Save TheSeamau5/07553149ba3d499e6436 to your computer and use it in GitHub Desktop.
Save TheSeamau5/07553149ba3d499e6436 to your computer and use it in GitHub Desktop.
Function to shuffle a list (useful to shuffle cards for example)
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
@jupp0r
Copy link

jupp0r commented Jul 12, 2016

I think it would be beneficial to not import List (...), because it's really hard to distinguish between functions and variables. Thanks, though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment