Skip to content

Instantly share code, notes, and snippets.

@latentflip
Created January 21, 2013 15:16
Show Gist options
  • Save latentflip/4586760 to your computer and use it in GitHub Desktop.
Save latentflip/4586760 to your computer and use it in GitHub Desktop.
and' :: [Bool] -> Bool
and' [] = True
and' (x:xs) = x && and' xs
concat' :: [[a]] -> [a]
concat' [] = []
concat' [[]] = []
concat' (x:xs) = x ++ concat' xs
replicate' :: Int -> a -> [a]
replicate' 0 a = []
replicate' n a = [a] ++ (replicate' (n-1) a)
(!!!) :: [a] -> Int -> a
(x:xs) !!! 0 = x
(x:xs) !!! n = xs !!! (n-1)
elem' :: Eq a => a -> [a] -> Bool
elem' a [] = False
elem' a (x:xs) = x == a || elem' a xs
merge :: [Int] -> [Int] -> [Int]
merge [] ys = ys
merge (x:xs) ys = merge xs (smaller ++ [x] ++ larger)
where
smaller = [ a | a <- ys, a <= x ]
larger = [ b | b <- ys, b > x ]
msort :: [Int] -> [Int]
msort [n] = [n]
msort xs = merge first second
where
l = length xs
h = l `div` 2
first = [a | (a,i) <- zip xs [0..l], i <= h]
second = [b | (b,j) <- zip xs [0..l], j > h]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment