Last active
June 7, 2018 06:09
-
-
Save yonta/18fbcf24139efeaa41038e30029b3656 to your computer and use it in GitHub Desktop.
How to get removed something and removed object, when you remove something from object in persistence data structure.
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
local | |
fun cleanHeadNone nil = nil | |
| cleanHeadNone (NONE :: t) = cleanHeadNone t | |
| cleanHeadNone (l as SOME _ :: _) = l | |
fun cleanEndNone l = (rev o cleanHeadNone o rev) l | |
fun simpleMin nil = raise Fail "Empty" | |
| simpleMin [NONE] = raise Fail "End with NONE" | |
| simpleMin [SOME x] = (x, nil) | |
| simpleMin (NONE :: t) = min t | |
| simpleMin (SOME x :: t) = | |
let | |
val (y, t') = min t | |
in | |
if x < y then (x, NONE :: t) | |
else (y, SOME x :: t') | |
end | |
in | |
(* | |
* int option list -> int * int option list | |
* ex) [NONE, SOME 2, SOME 3] -> (2, [NONE, NONE, SOME 3]) | |
*) | |
fun min l = cleanEndNone (simpleMin l) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment