refactor: idiomatic best-practices

This commit is contained in:
wi11-holdsworth 2025-10-08 15:54:14 +11:00
parent e16c6bbf7d
commit 17587190a5

View file

@ -57,10 +57,10 @@ module Proj2
)
where
import Data.Function (on)
import Data.Function
import Data.List
import Data.Map qualified as Map
import Data.Ord (comparing)
import Data.Ord
import Data.Set qualified as Set
-- ==== DATA STRUCTURES =======================================================
@ -107,10 +107,9 @@ instance Show Octave where
toPitch :: String -> Maybe Pitch
toPitch [note, octave] = Pitch <$> charToNote note <*> charToOctave octave
where
charToNote :: Char -> Maybe Note
charToNote c =
lookup
c
charToNote =
flip
lookup
[ ('A', A),
('B', B),
('C', C),
@ -119,10 +118,9 @@ toPitch [note, octave] = Pitch <$> charToNote note <*> charToOctave octave
('F', F),
('G', G)
]
charToOctave :: Char -> Maybe Octave
charToOctave c =
lookup
c
charToOctave =
flip
lookup
[ ('1', One),
('2', Two),
('3', Three)
@ -201,8 +199,8 @@ nextGuess (prevGuess, state) prevFeedback = (chosen, newState)
chosen = fst $ minimumBy (comparing snd) scored
newState = filter (/= chosen) candidates
scored = map (\x -> (x, score x candidates)) candidates
candidates = filter (\x -> prevFeedback == feedback prevGuess x) state
scored = map ((,) <*> (`score` candidates)) candidates
candidates = filter ((== prevFeedback) . feedback prevGuess) state
-- average number of possible targets per candidate
score candidate candidates = ((/) `on` fromIntegral) (sum l) (length l)
@ -225,7 +223,7 @@ matches :: (Eq a, Show a) => [a] -> [a] -> Int
matches xs ys = maximum permutationMatches
where
permutationMatches = map (pairwiseMatches xs) (permutations ys)
pairwiseMatches xs = length . filter (uncurry (==)) . zip xs
pairwiseMatches xs ys = length $ filter (uncurry (==)) $ zip xs ys
-- outputs a list of all possible chords, where a chord is a list of unique
-- pitches. this function happens to generate chords such that the pitches are
@ -236,12 +234,11 @@ matches xs ys = maximum permutationMatches
--
allChords :: [[Pitch]]
allChords =
[ chord
[ [p1, p2, p3]
| p1 <- allPitches,
p2 <- allPitches,
p3 <- allPitches,
let chord = [p1, p2, p3],
p1 < p2,
p3 <- allPitches,
p2 < p3
]
where