简体   繁体   English

字符串列表中的 Haskell 数组

[英]Haskell array from list of strings

So I have a list of strings and the task is to count how many times each string can be met in that list.所以我有一个字符串列表,任务是计算每个字符串在该列表中出现的次数。 I used maps:我使用了地图:

freqMap =  M.fromListWith (+) [(c, 1) | c <- subs]

and just sorting:并且只是排序:

frequency list = map (\l -> (length l, head l)) $ group (sort list)

but it's all too slow for my task - the original list is very large.但这对我的任务来说太慢了 - 原始列表非常大。 I've heard, that using unboxed arrays can be very fast.我听说,使用未装箱的数组可以非常快。 Like for list of integers:喜欢整数列表:

histogram bounds xs = accumArray (+) 0 bounds [(x, 1) | x <- xs]

As String is not a member of Ix class, the question is: is it possible to build an array from list of strings?由于 String 不是 Ix 类的成员,问题是:是否可以从字符串列表构建数组?

Data.HashMap (lazy/strict) is a faster version of the vanilla haskell maps - using them may speed up your operations if the bottleneck is primarily the update/lookup speed. Data.HashMap (lazy/strict) 是 vanilla haskell 映射的更快版本 - 如果瓶颈主要是更新/查找速度,使用它们可能会加快您的操作。

And the best part is that you can keep the nice clean approach you've already written, and not have to write (usually uglier) code that interacts with arrays.最好的部分是你可以保持你已经编写的漂亮干净的方法,而不必编写(通常是丑陋的)与数组交互的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM