Count occurrences of a substring: Difference between revisions

Content added Content deleted
m (→‎{{header|C sharp}}: Remove unnecessary type annotation)
(→‎{{header|Haskell}}: (or, reversing the order of arguments for ease of currying))
Line 986: Line 986:
2
2
</pre>
</pre>

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)
where
[n, h] = pack <$> [needle, haystack]

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

{{Out}}
<pre>[5,2,2]</pre>


=== List-based solution ===
=== List-based solution ===