Count occurrences of a substring: Difference between revisions

Content added Content deleted
Line 1,398: Line 1,398:


Alternatively, in a language built around currying, it might make more sense to reverse the suggested order of arguments.
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)
<lang haskell>{-# LANGUAGE OverloadedStrings #-}


import Data.Text hiding (length)
countAll :: String -> String -> Int
countAll needle haystack = length (breakOnAll n h)
where
[n, h] = pack <$> [needle, haystack]


--------- COUNT OF SUBSTRING INSTANCES IN A STRING -------

countAll :: Text -> Text -> Int
countAll needle haystack =
length
(breakOnAll needle haystack)

--------------------------- TEST -------------------------
main :: IO ()
main :: IO ()
main =
main =
print $
print $ countAll "ab" <$> ["ababababab", "abelian absurdity", "babel kebab"]</lang>
countAll "ab"

<$> [ "ababababab",
"abelian absurdity",
"babel kebab"
]</lang>
{{Out}}
{{Out}}
<pre>[5,2,2]</pre>
<pre>[5,2,2]</pre>