START ESCWYP MUSIC VIDEOS
------------------------------------------------- permut.hs -----------------------
-- To compile:
-- ghc -o permut -dynamic permut.hs

import System.Environment

jprind :: [Int] -> [Int] -> Int -> IO()
jprind [] _ _  = putStrLn ""
jprind (a:as) [] c  = do putStr (show (a:as))
                         putStr " | "
                         putStrLn (show c) 
jprind (a:as) (b:bs) c  = do putStr (show (a:as))
                             putStr " ... "
                             putStr (show (b:bs))
                             putStr " | "
                             putStrLn (show c) 

pst :: String -> Int
pst []                    = 0
pst (a:as) | (cti a) >= 0 = pst as
           | otherwise    = -1

sti :: String -> Int
sti [] = 0
sti (a:as) | (cti a) >= 0 = ((10^((length (a:as))-1)) * cti a) + sti as
           | otherwise = 0

cti :: Char -> Int
cti a | a == '1' = 1
      | a == '2' = 2
      | a == '3' = 3
      | a == '4' = 4
      | a == '5' = 5
      | a == '6' = 6
      | a == '7' = 7
      | a == '8' = 8
      | a == '9' = 9
      | a == '0' = 0
      | otherwise = -1

freespot :: [Int] -> Int -> [Int]
freespot [] a = []
freespot (x:xs) a | a == 0    = (1:xs)
                  | a < 0     = (x:xs)
                  | otherwise = x:freespot xs (a-1)

mf_fspot :: Int -> Int -> Int
mf_fspot new old | new >= 0 && new < old = new
                 | otherwise = old

help1 :: Int -> Int -> [Int] -> Int -> Int
help1 _ _ [] _ = 0
help1 start end (fs) old | (start < end) && (fs !! start == 1) = start 
                         | (start < end) && (fs !! start /= 1) = help1 (start+1) end fs old
                         | (start == end)                      = old

mf_tspot :: Int -> Int -> Int -> [Int] -> Int
mf_tspot new old num (fs) | new >= 0 && new == old = help1 (new+1) num fs old
                          | otherwise  = old

takespot :: [Int] -> Int -> [Int]
takespot [] a = []
takespot (x:xs) a | a == 0    = (0:xs)
                  | a < 0     = (x:xs)
                  | otherwise = x:takespot xs (a-1)

getstart :: Int -> Int -> [Int] -> Int
getstart _ _ [] = 0
getstart index minfree cs | (cs !! index)+1 == 0 = minfree
                          | otherwise            = (cs !! index)+1

numloop :: Int -> Int -> [Int] -> Int
numloop _ _ [] = 0
numloop start end fs | start < end && fs !! start == 1 = start
                     | start >= end = end
                     | otherwise = numloop (start+1) end fs

editcs :: [Int] -> Int -> Int -> [Int]
editcs [] index value = []
editcs (x:xs) index value | index == 0 = (value:xs)
                          | index < 0  = (x:xs)
                          | otherwise  = x:editcs xs (index-1) value 

permut :: Int -> Int -> [Int] -> [Int] -> Int -> Int -> Int -> Int -> Int -> IO()
permut _ _  [] _  _  _  _  _  _           = putStr ""
permut _ _  _  [] _  _  _  _  _           = putStr ""
permut a co fs cs nu id ol mf il | a == 0 = if id >=0 then permut 1 co fs cs nu id ol mf il
                                          else putStr ""
                               | a == 1 = permut 2 co fs cs nu id ol mf (getstart id mf cs) 
                               | a == 2 = permut 3 co fs cs nu id ol mf (numloop il nu fs) 
                               | a == 3 = if il < nu then permut 4 co fs cs nu id ol mf il
                                          else permut 9 co fs cs nu id ol mf il
                               | a == 4 = permut 5 co (freespot fs (cs !! id)) cs nu id ol (mf_fspot (cs !! id) mf) il
                               | a == 5 = permut 6 co (takespot fs il) (editcs cs id il) nu id ol (mf_tspot il mf nu fs) il
                               | a == 6 = if id < (nu-1) then permut 9 co fs cs nu (id+1) ol mf il
                                          else permut 8 co (freespot fs il) cs nu id ol (mf_fspot il mf) il
                               | a == 8 = do
                                             if nu < 50 then jprind cs [] (co+1)
                                             else jprind (take 10 cs) (drop (nu-10) cs) (co+1)
                                             permut 9 (co+1) fs cs nu id ol mf il
                               | a == 9 = if id == ol then permut 0 co (freespot fs (cs !! id)) (editcs cs id (-1)) nu (id - 1) (id - 1) (mf_fspot (cs !! id) mf) il
                                          else permut 0 co fs cs nu id id mf il

-- Usage:
-- permut 0 0 (take 6 [1,1..]) (take 6 [-1,-1..]) 6 0 0 0 0

main :: IO()
main = do
           a <- getArgs
           if a == [] || (pst (head a)) < 0 then 
              permut 0 0 (take 2 [1,1..]) (take 2 [-1,-1..]) 2 0 0 0 0
           else
              permut 0 0 (take (sti (head a)) [1,1..]) (take (sti (head a)) [-1,-1..]) (sti (head a)) 0 0 0 0

START ESCWYP MUSIC VIDEOS