-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotes.hs
More file actions
48 lines (39 loc) · 1.65 KB
/
Notes.hs
File metadata and controls
48 lines (39 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{-# LANGUAGE OverloadedStrings #-}
module Notes
( recommendNote
, recommendNotes
) where
import Data.List
import Data.Ord
import Data.Text (Text)
noteTypes :: [Text]
noteTypes = [ "AA", "A", "B", "C", "D", "E", "HR" ]
recommendNotes :: (Ord a, Fractional a, Integral b) => [Text] -> [a] -> b -> [Text]
recommendNotes _ _ 0 = []
recommendNotes notes targetDist 1 = [recommendNote notes targetDist]
recommendNotes notes targetDist n = nextNote : recommendNotes (nextNote:notes) targetDist (n-1)
where
nextNote = recommendNote notes targetDist
recommendNote :: (Num a, Ord a, Fractional a) => [Text] -> [a] -> Text
recommendNote notes targetDist
| length targetDist /= 7 = error "Invalid target distribution"
| sum targetDist /= 1 = error "Invalid target distribution"
| any (<0) targetDist = error "Invalid target distribution"
| all (==0) distributionDiff = snd (maximumBy (comparing fst) x)
| otherwise = snd (maximumBy (comparing fst) y)
where
x = zip targetDist noteTypes
y = zip distributionDiff noteTypes
distributionDiff = subtractLists targetDist currentDist
currentDist = distribution notes
subtractLists = zipWith (-)
distribution :: (Fractional a) => [Text] -> [a]
distribution = percentize . noteCount
percentize :: (Real a, Fractional b) => [a] -> [b]
percentize xs = map (/ sum fracList) fracList
where
fracList = map realToFrac xs
noteCount :: (Num a) => [Text] -> [a]
noteCount notes = [ count noteType notes | noteType <- noteTypes ]
count :: (Eq a, Num b) => a -> [a] -> b
count x = genericLength . filter (x==)