Skip to content

Instantly share code, notes, and snippets.

@jadlr
Last active February 22, 2016 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jadlr/c1026e4bd8e6bbbddd5c to your computer and use it in GitHub Desktop.
Save jadlr/c1026e4bd8e6bbbddd5c to your computer and use it in GitHub Desktop.
Permutations of a certain length of a list in haskell (v2)
module ListPermutations where
listPermutations :: [a] -> Integer -> [[a]]
listPermutations _ 0 = [[]]
listPermutations [] _ = []
listPermutations (x:xs) n = fmap (x:) (listPermutations xs (n - 1)) ++ listPermutations xs n
@jadlr
Copy link
Author

jadlr commented Feb 22, 2016

lazy, so this returns immediately:

*ListPermutations> take 2 $ listPermutations [1..1000] 15
[[1,2,3,4,5,6,7,8,9,10],[1,2,3,4,5,6,7,8,9,11]]

@lwld
Copy link

lwld commented Feb 22, 2016

👍

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