RosettaCodeData/Task/Comma-quibbling/Haskell/comma-quibbling-2.hs
2023-07-09 17:42:30 -04:00

25 lines
657 B
Haskell

import Data.List (intercalate)
--------------------- COMMA QUIBBLING --------------------
quibble :: [String] -> String
quibble ws@(_ : _ : _) =
intercalate
" and "
( [intercalate ", " . reverse . tail, head]
<*> [reverse ws]
)
quibble xs = concat xs
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_ (putStrLn . (`intercalate` ["{", "}"]) . quibble) $
[[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]]
<> ( words
<$> [ "One two three four",
"Me myself I",
"Jack Jill",
"Loner"
]
)