Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Created November 1, 2014 00:41
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 shigemk2/9bcb20647d96ac9f809d to your computer and use it in GitHub Desktop.
Save shigemk2/9bcb20647d96ac9f809d to your computer and use it in GitHub Desktop.
import Debug.Trace
-- 復路で再帰関数
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys)
| x < y = trace dbg1 $ x : merge xs (y:ys)
| otherwise = trace dbg2 $ y : merge (x:xs) ys
where
dbg1 = "x < y " ++ "x: " ++ show x ++ " y: " ++ show y ++ " " ++ show (x:xs) ++ show (y:ys)
dbg2 = "otherwise " ++ "x: " ++ show x ++ " y: " ++ show y ++ " " ++ show (x:xs) ++ show (y:ys)
msort [] = []
msort [x] = [x]
-- 往路で分割
msort xs = trace dbg1 $ merge (msort (take h xs)) (msort (drop h xs))
where
dbg1 = "xs: " ++ show xs
h = (length xs) `div` 2
main = do
print $ msort [4, 6, 9, 8, 3, 5, 1, 7, 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment