- Haskell Data Analysis Cookbook
- Nishant Shukla
- 205字
- 2021-12-08 12:43:36
Implementing a frequency table using Data.MultiSet
A frequency map of values is often useful to detect outliers. We will use an existing library that does much of the work for us.
Getting ready
We will be using the multiset
package from Hackage:
$ cabal install multiset
How to do it...
Create a new file, which we will call Main.hs
, and perform the following steps:
- We will use the
fromList
andtoOccurList
functions fromData.MultiSet
:import Data.MultiSet (fromList, toOccurList)
- Define a simple data type for colors:
data Color = Red | Green | Blue deriving (Show, Ord, Eq)
- Create a list of these colors:
main :: IO () main = do let items = [Red, Green, Green, Blue, Red, Green, Green]
- Implement the frequency map and print it out:
let freq = toOccurList . fromList $ items print freq
- Run the code to display the frequency list:
$ runhaskell Main.hs [ (Red, 2), (Green, 4), (Blue, 1) ]
How it works...
The toOccurList :: MultiSet a -> [(a, Int)]
function creates a frequency map from a list. We construct MuliSet
using the provided fromList
function.
See also
If importing a new library is not desired, see the previous recipe on Implementing a frequency map using Data.List.
推薦閱讀
- Node.js Design Patterns
- Boost C++ Application Development Cookbook(Second Edition)
- 深入淺出Java虛擬機(jī):JVM原理與實(shí)戰(zhàn)
- 網(wǎng)絡(luò)爬蟲原理與實(shí)踐:基于C#語言
- Linux命令行與shell腳本編程大全(第4版)
- Advanced Oracle PL/SQL Developer's Guide(Second Edition)
- Building RESTful Python Web Services
- Learning Probabilistic Graphical Models in R
- Java程序設(shè)計入門
- Go語言底層原理剖析
- Mockito Essentials
- IPython Interactive Computing and Visualization Cookbook
- Practical Maya Programming with Python
- Spring Data JPA從入門到精通
- PHP項(xiàng)目開發(fā)全程實(shí)錄(第4版)