Advertisement
Yurry

Idris random shuffle

Oct 23rd, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Prelude.Vect
  2. import Effect.Random
  3.  
  4. extractAt : (n : Fin (S k)) -> Vect (S k) a -> (a, Vect k a)
  5. extractAt n xs = (Vect.index n xs, Vect.deleteAt n xs)
  6.  
  7. extractRandom : Vect (S k) a -> { [RND] } Eff (a, Vect k a)
  8. extractRandom {k} xs = return $ extractAt !(rndFin k) xs
  9.  
  10. shuffle : Vect n a -> { [RND] } Eff (Vect n a)
  11. shuffle xs = go [] xs where
  12.     go : Vect n a -> Vect m a -> { [RND] } Eff (Vect (n + m) a)
  13.     go {n} res [] = rewrite plusZeroRightNeutral n in return res
  14.     go {n} {m = S m} tmp rest = rewrite sym $ plusSuccRightSucc n m in do
  15.         (e, new) <- extractRandom rest
  16.         go (e :: tmp) new
  17.    
  18. extractSome : (n : Nat) -> Vect (n + m) a -> { [RND] } Eff (Vect n a)
  19. extractSome n xs = return $ take n !(shuffle xs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement