Last active
August 29, 2015 14:26
-
-
Save nobsun/e0506551e758893497ed to your computer and use it in GitHub Desktop.
順列列挙関数(素朴な実装) ref: http://qiita.com/nobsun/items/babd28fe81ba3b9f304f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
perms :: [a] -> [[a]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
perms [] = [[]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
perms (x:xs) = concatMap (insert x) (perms xs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
perms :: [a] -> [[a]] | |
perms [] = [[]] | |
perms (x:xs) = concatMap (insert x) (perms xs) | |
insert :: a -> [a] -> [[a]] | |
insert = undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
insert x xs = map (ins x) (divids xs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
perms :: [a] -> [[a]] | |
perms [] = [[]] | |
perms (x:xs) = concatMap (insert x) (perms xs) | |
insert :: a -> [a] -> [[a]] | |
insert = map (ins x) (divids xs) | |
ins :: a -> ([a],[a]) -> [a] | |
ins x (ys,zs) = ys ++ [x] ++ zs | |
divids :: [a] -> [([a],[a])] | |
divids = undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
divids [] = [([],[])] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
divids (x:xs) = ([],x:xs) : [ (x:ys, zs) | (ys,zs) <- divids xs ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
perms :: [a] -> [[a]] | |
perms [] = [[]] | |
perms (x:xs) = concatMap (insert x) (perms xs) | |
insert :: a -> [a] -> [[a]] | |
insert x xs = map (ins x) (divids xs) | |
ins :: a -> ([a],[a]) -> [a] | |
ins x (ys,zs) = ys ++ x : zs | |
divids :: [a] -> [([a],[a])] | |
divids [] = [([],[])] | |
divids xxs@(x:xs) = ([],xxs) : [ (x:ys,zs) | (ys,zs) <- divids xs ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment