Advertisement
Jakowlew

Untitled

Dec 25th, 2020
1,944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Main where
  2.  
  3. import Data.List (group, sort)
  4.  
  5. main :: IO ()
  6. main = getLine >>= mapM_ putStrLn . palindromes
  7.  
  8. palindromes :: String -> [String]
  9. palindromes [] = [""]
  10. palindromes [x] = [[x]]
  11. palindromes xs | head xs == last xs = (++)
  12.                                       <$> ((++)<$> [[head xs]] <*> (palindromes . tail . init) xs)
  13.                                       <*> [[last xs]]
  14.                | otherwise          = candidates $ branches xs
  15.     where
  16.         branches xs = map head . group . sort . concatMap (palindromes . ($xs)) $ [tail, init, tail . init]
  17.         maxlen = maximum . map length
  18.         candidates xs = filter (\x -> length x == maxlen xs) xs
  19.  
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement