Created
March 3, 2017 16:14
-
-
Save xiaohanyu/44048f5ecf03016fdf468b956c6dfc6a to your computer and use it in GitHub Desktop.
Haskell permutation generation algorithm with list comprehension.
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
import Data.List | |
perm :: (Eq a) => [a] -> Int -> [[a]] | |
perm _ 0 = [[]] | |
perm xs r | length xs < r = [[]] | |
| otherwise = [x:ys | x <- xs, ys <- perm (delete x xs) (r - 1)] |
Hi, @readamd256,
To be honest, I forgot how I came up with this code snippets. But I think this gist is just a step of generalisation of your code.
Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your Haskell implementation is brilliant!!! It is similar to the nPn Haskell version at https://stackoverflow.com/questions/49280797/permutation-algorithm-in-haskell
namely,
However, yours is nPr or P(n,r), i.e., "n choose r", which is more general. I have been working on such an algorithm for a while. Do you have any suggestions regarding how you arrived at your solution/implementation?
Thank you.
Richard E. Adams
Email: [email protected]