Jump to content

Count occurrences of a substring: Difference between revisions

m
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.TextLANGUAGE hidingOverloadedStrings (length)#-}
 
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 :: StringText -> StringText -> Int
countAll needle haystack = length (breakOnAll n h)
length
[n,(breakOnAll h] = pack <$> [needle, haystack])
 
--------------------------- TEST -------------------------
main :: IO ()
main =
print $
print $ countAll "ab" <$> ["ababababab", "abelian absurdity", "babel kebab"]</lang>
countAll "ab"
 
<$> [ "ababababab",
"abelian absurdity",
"babel kebab"
]</lang>
{{Out}}
<pre>[5,2,2]</pre>
9,655

edits

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