RosettaCodeData/Task/Topswops/Haskell/topswops-2.hs

22 lines
599 B
Haskell

import Data.List (permutations, inits)
import Control.Arrow (first)
derangements :: [Int] -> [[Int]]
derangements = (filter . (and .) . zipWith (/=)) <*> permutations
topswop :: Int -> [a] -> [a]
topswop = ((uncurry (++) . first reverse) .) . splitAt
topswopIter :: [Int] -> [[Int]]
topswopIter = takeWhile ((/= 1) . head) . iterate (topswop =<< head)
swops :: [Int] -> [Int]
swops = fmap (length . topswopIter) . derangements
topSwops :: [Int] -> [(Int, Int)]
topSwops = zip [1 ..] . fmap (maximum . (0 :) . swops) . tail . inits
main :: IO ()
main = mapM_ print $ take 10 $ topSwops [1 ..]