Skip to content

Instantly share code, notes, and snippets.

@nnabeyang
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nnabeyang/8701630 to your computer and use it in GitHub Desktop.
Save nnabeyang/8701630 to your computer and use it in GitHub Desktop.
ユークリッド互除法の計算過程をControl.Monad.Writerで ref: http://qiita.com/nnabeyang/items/4667ed2ed5aa2a618e28
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