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