Count occurrences of a substring: Difference between revisions
m
→{{header|Haskell}}
Line 1,398:
Alternatively, in a language built around currying, it might make more sense to reverse the suggested order of arguments.
<lang haskell>
import Data.Text hiding (length)
countAll :: String -> String -> Int▼
countAll needle haystack = length (breakOnAll n h)▼
[n, h] = pack <$> [needle, haystack]▼
--------- COUNT OF SUBSTRING INSTANCES IN A STRING -------
length
--------------------------- TEST -------------------------
main :: IO ()
main =
print $
countAll "ab"
<$> [ "ababababab",
"abelian absurdity",
"babel kebab"
]</lang>
{{Out}}
<pre>[5,2,2]</pre>
|