Changing move function signature to match state method in preparation for next presentation

This commit is contained in:
Logan McGrath 2021-10-06 12:19:22 -07:00
parent 6d5e78c0a9
commit cd9b71c03e

View File

@ -1,5 +1,7 @@
module Homework.Ch01.Hanoi where
import Data.Maybe
newtype Peg = Peg ()
data Move = Move {moveFrom :: String, moveTo :: String} deriving (Eq, Show)
@ -15,9 +17,9 @@ hanoi numDisks pegLabelA pegLabelB pegLabelC =
(pegLabelB, []),
(pegLabelC, [])
]
in Right $ snd $ move pegs
in Right . return . fromJust . fst $ move pegs
move :: Pegs -> (Pegs, [Move])
move :: Pegs -> (Maybe Move, Pegs)
move pegs =
let (firstPegLabel, firstPeg) = head pegs
(lastPegLabel, lastPeg) = last pegs
@ -26,13 +28,13 @@ move pegs =
canMove = firstPegDisc < lastPegDisc
in if canMove
then
( [ (firstPegLabel, init firstPeg),
( Just $ Move firstPegLabel lastPegLabel,
[ (firstPegLabel, init firstPeg),
head $ tail pegs,
(lastPegLabel, lastPeg <> [firstPegDisc])
],
[Move firstPegLabel lastPegLabel]
]
)
else (pegs, [])
else (Nothing, [])
fillPegWithDiscs :: Int -> [Disc]
fillPegWithDiscs numDisks = Disc <$> reverse [1 .. numDisks]