style: format code

This commit is contained in:
wi11-holdsworth 2025-09-24 12:17:28 +10:00
parent f49eba9676
commit 3654485f23
2 changed files with 23 additions and 22 deletions

16
Main.hs
View file

@ -10,13 +10,11 @@ import Data.List
import Data.Maybe import Data.Maybe
import Proj2 import Proj2
-- | Guess the given target, counting and showing the guesses. -- | Guess the given target, counting and showing the guesses.
guessTest :: [Pitch] -> IO () guessTest :: [Pitch] -> IO ()
guessTest target = do guessTest target = do
let (guess,other) = initialGuess let (guess, other) = initialGuess
loop target guess other 1 loop target guess other 1
-- | Given a target and guess and a guess number, continue guessing -- | Given a target and guess and a guess number, continue guessing
-- until the right target is guessed. -- until the right target is guessed.
@ -25,19 +23,17 @@ loop target guess other guesses = do
putStrLn $ "Your guess #" ++ show guesses ++ ": " ++ show guess putStrLn $ "Your guess #" ++ show guesses ++ ": " ++ show guess
let answer = feedback target guess let answer = feedback target guess
putStrLn $ " My answer: " ++ show answer putStrLn $ " My answer: " ++ show answer
if answer == (3,0,0) if answer == (3, 0, 0)
then do then do
putStrLn $ "You got it in " ++ show guesses ++ " guesses!" putStrLn $ "You got it in " ++ show guesses ++ " guesses!"
else do else do
let (guess',other') = nextGuess (guess,other) answer let (guess', other') = nextGuess (guess, other) answer
loop target guess' other' (guesses+1) loop target guess' other' (guesses + 1)
-- | Parse a string containing a number of space-separated pitches to produce -- | Parse a string containing a number of space-separated pitches to produce
-- a list of pitches. Error if any of the pitches can't be parsed. -- a list of pitches. Error if any of the pitches can't be parsed.
toChord :: String -> [Pitch] toChord :: String -> [Pitch]
toChord = (fromJust . mapM toPitch . words) toChord = fromJust . mapM toPitch . words
-- | Prompt for a target and use guessTest to try to guess it. -- | Prompt for a target and use guessTest to try to guess it.
main :: IO () main :: IO ()

View file

@ -1,26 +1,31 @@
--Implement your solution here --
--SEE THE PROJECT CODING GUIDELINES ON THE LMS FOR DETAILS OF
--THE CRITERIA THAT WILL BE EMPLOYED IN ASSESSING YOUR CODE.
--Please DELETE THIS WHOLE COMMENT, and write your own.
module Proj2 (Pitch, toPitch, feedback, module Proj2
GameState, initialGuess, nextGuess) where ( Pitch,
toPitch,
feedback,
GameState,
initialGuess,
nextGuess,
)
where
type GameState = () type GameState = ()
data Pitch = Pitch { note :: Char data Pitch = Pitch
, octave :: Int { note :: Char,
} deriving (Eq) octave :: Int
}
deriving (Eq)
instance Show Pitch where instance Show Pitch where
show (Pitch note octave) = [note] ++ show octave show (Pitch note octave) = note : show octave
toPitch :: String -> Maybe Pitch toPitch :: String -> Maybe Pitch
toPitch _ = Just (Pitch 'A' 1) toPitch _ = Just (Pitch 'A' 1)
feedback :: [Pitch] -> [Pitch] -> (Int, Int, Int) feedback :: [Pitch] -> [Pitch] -> (Int, Int, Int)
feedback _ _ = (0,0,0) feedback _ _ = (0, 0, 0)
initialGuess :: ([Pitch], GameState) initialGuess :: ([Pitch], GameState)
initialGuess = ([Pitch 'A' 1], ()) initialGuess = ([Pitch 'A' 1], ())