Skip to content

Instantly share code, notes, and snippets.

@xiaohanyu
Created March 3, 2017 16:14
Show Gist options
  • Save xiaohanyu/44048f5ecf03016fdf468b956c6dfc6a to your computer and use it in GitHub Desktop.
Save xiaohanyu/44048f5ecf03016fdf468b956c6dfc6a to your computer and use it in GitHub Desktop.
Haskell permutation generation algorithm with list comprehension.
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)]
@xiaohanyu
Copy link
Author

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.

@readams256
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment