Created
November 16, 2013 16:07
-
-
Save nobsun/7501963 to your computer and use it in GitHub Desktop.
空ではないリストから最小の要素を1つだけ削除する ref: http://qiita.com/nobsun/items/49440368b85c36ec4d42
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
deleteMin :: Ord a => [a] -> [a] | |
deleteMin xs = case break (== minimum xs) xs of | |
(ys,_:zs) -> ys++zs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
deleteMin :: Ord a => [a] -> [a] | |
deleteMin = uncurry (++) . second tail . flip break <*> ((==) . minimum) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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