SHOW:
|
|
- or go back to the newest paste.
| 1 | import Data.Ord | |
| 2 | import Data.List | |
| 3 | ||
| 4 | -- | Sorting a List by frequency of their Elements | |
| 5 | sortByFreq :: Eq a => [a] -> [a] | |
| 6 | sortByFreq lis = sortF $ getFreq lis $ map (\x -> (x, 0)) $ nub lis | |
| 7 | where getFreq [] freq = freq | |
| 8 | getFreq (x:xs) freq = getFreq xs $ map (\(elem, freq) -> | |
| 9 | if x == elem | |
| 10 | then (elem, freq + 1) | |
| 11 | else (elem, freq)) freq | |
| 12 | sortF = sortBy (comparing snd) |