Last active
August 29, 2015 13:55
-
-
Save nnabeyang/8701630 to your computer and use it in GitHub Desktop.
ユークリッド互除法の計算過程をControl.Monad.Writerで ref: http://qiita.com/nnabeyang/items/4667ed2ed5aa2a618e28
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 | |
gcd' :: Int -> Int -> Writer [String] Int | |
gcd' a b | |
|b == 0 = do | |
tell["= " ++ show a] | |
return(a) | |
|otherwise = do | |
let nb = a `mod` b | |
expr = "gcd(" ++ show(b) ++ ", " ++ show(nb) ++ ")" | |
msg = show(a) ++ " = " ++ show(b) ++ " * " ++ show(a `div` b) ++ " + " ++ show(nb) | |
in (tell["= " ++ expr ++ " [" ++ msg ++ "]"]) | |
gcd' b (a `mod` b) | |
show_gcd :: (Int, Int) -> IO () | |
show_gcd (a, b) = mapM_ putStrLn $ snd $ runWriter (gcd' a b) | |
fib :: Int -> Int | |
fib 1 = 1 | |
fib 2 = 1 | |
fib n = fib (n-1) + fib (n-2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment