import Control.SetFunctions import Data.List ( nub ) -- Permutations with functional patterns perm :: Data a => [a] -> [a] perm [] = [] perm (xs ++ [x] ++ ys) = x : perm (xs ++ ys) -- Are there two adjacent elements out of order? someDescending :: [Int] -> Bool --someDescending (_ ++ [x,y] ++ _) | x>y = True someDescending (_ ++ x : y : _) | x>y = True -- A sorted list of a given list is a permutation which is not "out of order": sort :: [Int] -> [Int] sort xs | isEmpty ((set1 someDescending) p) = p where p = perm xs ------------------------------------------------------------------------- -- ACM programming contest: -- find the length of a given list until the first repeated element -- lengthUpToRepeat [1,2,3,42,56,87,2,3,1] --> 7 lengthUpToRepeat :: [Int] -> Int lengthUpToRepeat (xs ++ [x] ++ _) | x `elem` xs && nub xs == xs = length xs + 1