Skip to content

Instantly share code, notes, and snippets.

@ChinaXing
Last active August 29, 2015 14:22
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 ChinaXing/63cd1fd9dd01bbbd881d to your computer and use it in GitHub Desktop.
Save ChinaXing/63cd1fd9dd01bbbd881d to your computer and use it in GitHub Desktop.
make fibonacci graphviz dot file
import Debug.Trace
import System.Environment
import System.IO
newtype S a = S (Int, a) deriving (Show)
fib' :: S Int -> Int
fib' (S (s, 0)) = trace (show s ++ " [ label = " ++ show 0 ++ "]") 1
fib' (S (s, 1)) = trace (show s ++ " [ label = " ++ show 1 ++ "]") 1
fib' (S (s, n)) = trace (show s ++ " [label = " ++ show n ++ "]") f1 + f2
where
f1 = trace (show s ++ " -> " ++ show (s * 2 + 0)) $ fib' (S (s * 2 + 0, n - 1))
f2 = trace (show s ++ " -> " ++ show (s * 2 + 1)) $ fib' (S (s * 2 + 1, n - 2))
fib :: Int -> Int
fib n = fib' (S (1, n))
main :: IO ()
main = do
[n] <- getArgs
hPutStrLn stderr "digraph fib {"
putStrLn $ show $ fib $ read n
hPutStrLn stderr "}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment