Jump to content

Comma quibbling: Difference between revisions

m
Line 2,013:
Or, defining just two cases, and drawing more on standard libraries than on hand-crafted pattern-matching and recursion:
<syntaxhighlight lang="haskell">import Data.List (intercalate)
 
--------------------- COMMA QUIBBLING --------------------
 
quibble :: [String] -> String
quibble ws@(_ : _ : _) =
intercalate
| length ws > 1 =
intercalate" and "
( [intercalate ", " . reverse . tail, head]
" and "
([intercalate ", " . reverse . tail, head] <*> [reverse ws])
)
| otherwise = concat ws
quibble xs = concat xs
 
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_ (putStrLn . (`intercalate` ["{", "}"]) . quibble) $
[[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]] ++
"<> and( "words
(words <$> ["One two three four", "Me myself I", "Jack Jill", "Loner"])
<$> [ "One two three four",
</syntaxhighlight>
"Me myself I",
"Jack Jill",
"Loner"
]
)</syntaxhighlight>
{{Out}}
<pre>{}
9,655

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.