Skip to content

Instantly share code, notes, and snippets.

@gergoerdi
Created November 11, 2015 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gergoerdi/8dda25d8347c26e1d475 to your computer and use it in GitHub Desktop.
Save gergoerdi/8dda25d8347c26e1d475 to your computer and use it in GitHub Desktop.
import Data.Vect
%default total
permutations : Vect n a -> Vect (fact n) (Vect n a)
permutations [] = [[]]
permutations {n = S n} (x :: xs) = rewrite multCommutative (S n) (fact n) in
concat $ map (inserts x) (permutations xs)
where
inserts : a -> Vect k a -> Vect (S k) (Vect (S k) a)
inserts x [] = [[x]]
inserts x (y :: ys) = (x :: y :: ys) :: (map (y ::) (inserts x ys))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment