add permutations

This commit is contained in:
rnhmjoj 2016-11-27 20:15:33 +01:00
parent 44aec1f56c
commit 99619b7109
No known key found for this signature in database
GPG Key ID: 362BB82B7E496B7C

View File

@ -179,6 +179,20 @@ transpose (xs :- xss) = gcastWith proof (zipWith (:-) xs (transpose xss))
where proof = min_self (sLength xs)
-- | The 'permutations' function returns the vector of all permutations of the argument.
--
-- > permutations "abc" ≡ ["abc","bac","cba","bca","cab","acb"]
permutations Vec a n Vec (Vec a n) (Fact n)
permutations Nil = Nil:-Nil
permutations (x:-Nil) = (x:-Nil):-Nil
permutations xs@(_:-_) = concatMap (\(y,ys) map (y:-) (permutations ys)) (select xs)
where
select Vec a n Vec (a, Vec a (Pred n)) n
select Nil = Nil
select (x:-Nil) = (x, Nil) :- Nil
select (x:-xs@(_:-_)) = (x,xs) :- map (\(y,ys) (y, x:-ys)) (select xs)
-- | 'foldl' applied to a binary operator, a starting value
-- and a 'Vec' reduces the vector to a single value obtained
-- by sequentially applying the operation from the left to the right.