Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何计算字符串中字符串的出现次数(Haskell)_Haskell - Fatal编程技术网

如何计算字符串中字符串的出现次数(Haskell)

如何计算字符串中字符串的出现次数(Haskell),haskell,Haskell,Haskell中是否有类似的函数库 > function "bal bla hu bla" ["bla","bal"] [(2,"bla"),(1,"bal")] Data.Text提供了一个函数,但是它在Text而不是String上工作,因此我们必须使用 (未经测试。)使用: 及 UPD: 在这里也很有用 {-# LANGUAGE NoMonomorphismRestriction, FlexibleContexts #-} import Control

Haskell中是否有类似的函数库

> function "bal bla hu bla" ["bla","bal"]
[(2,"bla"),(1,"bal")]

Data.Text
提供了一个函数,但是它在
Text
而不是
String
上工作,因此我们必须使用

(未经测试。)

使用:


UPD

在这里也很有用

{-# LANGUAGE NoMonomorphismRestriction,
             FlexibleContexts
  #-}
import Control.Arrow ((&&&))
import Data.Either (partitionEithers)
import Text.Parsec

occs :: String -> [String] -> [(Int, String)]
occs s = map (countP s &&& id)

countP str substr = either (const 0) occsNumber $ parse (parseMany substr) "" str
  where
    occsNumber = length . snd . partitionEithers

parseSingle :: Stream s m Char => String -> ParsecT s u m (Either Char String)
parseSingle s = fmap Right (try (string s)) <|> fmap Left anyChar

parseMany :: Stream s m Char => String -> ParsecT s u m [Either Char String]
parseMany = many . parseSingle
“火车失事”解决方案:

import Data.List

f txt ws = map freq $ filter isElem $ group $ sort $ words txt where
    isElem (w:_) = w `elem` ws
    freq xs@(x:_) = (length xs, x)
从那里,你可以去“一元”

导入数据。列表
进口管制
f txt ws=do

xs@(x:)OMG OMG!到目前为止给出的每个解都具有二次计算复杂性!(即使是
Data.Text
one也有最坏的二次运行时间)

显然,您需要使用自己的字符串搜索算法

这是我的照片。我认为这是一个变化的


尽管如此,仍有很大的改进空间!如果我们通过构建前缀树或类似的方法对输入字符串进行预处理,则搜索多个模式比逐个搜索更有效…

有效,除了
yourFunction
需要
(Eq a)
作为约束。我们应该补充一点,使用
Text
而不是
String
可能是个好主意……您不需要
count
中的
s
参数,因为它是闭包,可以访问
str
。较新版本的文本具有平均大小写O(m+n)时间复杂度。
occs ∷ Eq a ⇒ [a] → [[a]] → [(Int, [a])]
occs str = map (count str &&& id)
  where
    count s x = length (splitOn x s) - 1
 >occs "bal bla hu bla" ["bla","bal"]
 [(2,"bla"),(1,"bal")]
{-# LANGUAGE NoMonomorphismRestriction,
             FlexibleContexts
  #-}
import Control.Arrow ((&&&))
import Data.Either (partitionEithers)
import Text.Parsec

occs :: String -> [String] -> [(Int, String)]
occs s = map (countP s &&& id)

countP str substr = either (const 0) occsNumber $ parse (parseMany substr) "" str
  where
    occsNumber = length . snd . partitionEithers

parseSingle :: Stream s m Char => String -> ParsecT s u m (Either Char String)
parseSingle s = fmap Right (try (string s)) <|> fmap Left anyChar

parseMany :: Stream s m Char => String -> ParsecT s u m [Either Char String]
parseMany = many . parseSingle
> occs "bal bla hu bla" ["bla","bal"]
[(2,"bla"),(1,"bal")]
import Data.List

f txt ws = map freq $ filter isElem $ group $ sort $ words txt where
    isElem (w:_) = w `elem` ws
    freq xs@(x:_) = (length xs, x)
import Data.List
import Control.Monad

f txt ws = do
    xs@(x:_) <- group $ sort $ words txt 
    guard $ x `elem` ws
    return (length xs, x)
data Searcher = Found | Initial Searcher | Searching Char Searcher Searcher

runSearcher :: Searcher -> Char -> Searcher
runSearcher (Searching c suc fail) s | c == s = suc
 | otherwise = runSearcher fail s
runSearcher (Initial s) _ = s

mkSearcher pattern = initial where
  initial = go (Initial initial) pattern

  go fallback [] = Found
  go fallback (c:t) = Searching c (go (runSearcher fallback c) t) fallback

search :: String -> String -> Integer
search pat = go searcher where
  searcher = mkSearcher pat
  go Found s = 1 + go searcher s
  go src (c:t) = go (runSearcher src c) t
  go src [] = 0