Skip to content

Instantly share code, notes, and snippets.

@nobsun
Created November 16, 2013 16:07
Show Gist options
  • Save nobsun/7501963 to your computer and use it in GitHub Desktop.
Save nobsun/7501963 to your computer and use it in GitHub Desktop.
空ではないリストから最小の要素を1つだけ削除する ref: http://qiita.com/nobsun/items/49440368b85c36ec4d42
deleteMin :: Ord a => [a] -> [a]
deleteMin xs@(x:_) = snd $ para f (x,[]) xs
where
f x (xs,(y,_))
| x < y = (x,xs)
| otherwise = (y,x:xs)
para :: (a -> ([a],b) -> b) -> b -> [a] -> b
para _ z [] = z
para f z (x:xs) = f x (xs, para f z xs)
deleteMin :: Ord a => [a] -> [a]
deleteMin xs = case break (== minimum xs) xs of
(ys,_:zs) -> ys++zs
deleteMin :: Ord a => [a] -> [a]
deleteMin = uncurry (++) . second tail . flip break <*> ((==) . minimum)
deleteMin :: Ord a => [a] -> [a]
deleteMin = snd . rec
where
rec [x] = (x,[])
rec (x:xs@(_:_))
| x <= y = (x,xs)
| otherwise = (y,x:ys)
where
(y,ys) = rec xs
deleteMin :: Ord a => [a] -> [a]
deleteMin = snd . (flip (para f) <*> (,[]) . head)
where
f x (xs,(y,ys))
| x <= y = (x,xs)
| otherwise = (y,x:ys)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment