add permutations
This commit is contained in:
parent
44aec1f56c
commit
99619b7109
@ -179,6 +179,20 @@ transpose (xs :- xss) = gcastWith proof (zipWith (:-) xs (transpose xss))
|
|||||||
where proof = min_self (sLength xs)
|
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
|
-- | 'foldl' applied to a binary operator, a starting value
|
||||||
-- and a 'Vec' reduces the vector to a single value obtained
|
-- and a 'Vec' reduces the vector to a single value obtained
|
||||||
-- by sequentially applying the operation from the left to the right.
|
-- by sequentially applying the operation from the left to the right.
|
||||||
|
Loading…
Reference in New Issue
Block a user