Skip to content

Instantly share code, notes, and snippets.

@weskerfoot
Created November 1, 2012 03:48
Show Gist options
  • Save weskerfoot/3991563 to your computer and use it in GitHub Desktop.
Save weskerfoot/3991563 to your computer and use it in GitHub Desktop.
Haskell - permutations
import Control.Monad
enumerate xs = zip [1..] xs
dlists :: [a] -> [(a, [a])]
dlists xs = [(x, [y | (j, y) <- enumerate xs, j /= i]) | (i, x) <- enumerate xs]
permute (x:y:[]) = [y:x:[], x:y:[]]
permute xs = let difflist = dlists xs
in join [map (i:) (permute d) | (i, d) <- difflist]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment