Created
December 15, 2015 21:20
-
-
Save CGA1123/ee94448943795093bffe to your computer and use it in GitHub Desktop.
100 Doors Problem in SML
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
(* 100 doors *) | |
fun listOf 0 _ = [] | |
| listOf n e = e::(listOf (n-1) e); | |
fun invert x = not x; | |
fun flip _ _ [] = [] | |
| flip 0 _ _ = [] | |
| flip s n (h::t) = if (s=n) then (invert h)::(flip s 1 t) else h::(flip s (n+1) t); | |
fun flipThrough 100 (h::t) = (h::t) | |
| flipThrough n (h::t) = flipThrough (n+1) (flip n 1 (h::t)); | |
fun onehundreddoors () = let | |
fun answer [] _ = [] | |
| answer (h::t) n = if h then n::(answer t (n+1)) else answer t (n+1); | |
in answer (flipThrough 1 (listOf 100 false)) 1 end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment