diff options
author | Camil Staps | 2016-02-12 15:01:00 +0100 |
---|---|---|
committer | Camil Staps | 2016-02-12 15:01:00 +0100 |
commit | efd533331d6a7f0c51ef857af448a6c84c3084ed (patch) | |
tree | 7f28f4e20a215784f27643ad49029332204528b2 /Assignment1/CamilStaps-assignment1-freqs.hs | |
parent | Makefile (diff) |
Removed spaces in path
Diffstat (limited to 'Assignment1/CamilStaps-assignment1-freqs.hs')
-rw-r--r-- | Assignment1/CamilStaps-assignment1-freqs.hs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Assignment1/CamilStaps-assignment1-freqs.hs b/Assignment1/CamilStaps-assignment1-freqs.hs new file mode 100644 index 0000000..6c6cd47 --- /dev/null +++ b/Assignment1/CamilStaps-assignment1-freqs.hs @@ -0,0 +1,39 @@ +import System.Environment +import Data.String.Utils +import qualified Data.List as List + +main = do + args <- getArgs + let input = replace " " "" (head args) + print $ List.reverse $ List.sort $ countfreqs input 1 [] + print $ List.reverse $ List.sort $ countfreqs input 2 [] + print $ List.reverse $ List.sort $ countfreqs input 3 [] + +-- countfreqs s n []: calculate occurrence statistics of n-grams in s +countfreqs :: String -> Int -> [Freq] -> [Freq] +countfreqs "" _ fl = fl +countfreqs s n fl + | length s < n = fl + | otherwise = countfreqs (tail s) n (freqsincr fl (take n s) []) + +-- freqsincr fl s []: add s to the frequencies in fl +freqsincr :: [Freq] -> String -> [Freq] -> [Freq] +freqsincr [] s fl = fl ++ [Freq { item = s, freq = 1}] +freqsincr (freq:fla) s2 flb + | get_item freq == s2 = fla ++ flb ++ [Freq { item = s2, freq = get_freq freq + 1}] + | otherwise = freqsincr fla s2 (flb ++ [Freq { item = get_item freq, freq = get_freq freq}]) + +-- Data type for keeping track of frequencies (Int) of substrings (String) +data Freq = Freq { item :: String, freq :: Int } deriving (Eq) + +get_item :: Freq -> String +get_item = item + +get_freq :: Freq -> Int +get_freq = freq + +instance Ord Freq where + f1 `compare` f2 = get_freq f1 `compare` get_freq f2 + +instance Show Freq where + show f = get_item f ++ " (" ++ show (get_freq f) ++ "x)"
\ No newline at end of file |