Created
December 11, 2018 08:04
-
-
Save lironsade/1643f27edc48843343b56bceb9895fa8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad.Writer | |
type Tower = ([Int], Char) | |
data Hanoi = Hanoi Tower Tower Tower deriving Show | |
move :: Hanoi -> Hanoi | |
move (Hanoi a b c) = Hanoi (init (fst a), snd a) b ((fst c) ++ [last (fst a)], snd c) | |
solve :: Hanoi -> Writer [String] Hanoi | |
solve (Hanoi a b c) = solve' (length (fst a)) (Hanoi a b c) | |
solve' :: Int -> Hanoi -> Writer [String] Hanoi | |
solve' 1 (Hanoi a b c) = do | |
tell ["Moved " ++ [snd a] ++" to " ++ [snd c]] | |
return (move (Hanoi a b c)) | |
solve' n (Hanoi a b c) = do | |
(Hanoi x y z) <- solve' (n - 1) (Hanoi a c b) | |
(Hanoi p q r) <- solve' 1 (Hanoi x z y) | |
(Hanoi aa bb cc) <- solve' (n - 1) (Hanoi q p r) | |
return (Hanoi bb aa cc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment