Skip to content

Instantly share code, notes, and snippets.

@CGA1123
Created December 15, 2015 21:20
Show Gist options
  • Save CGA1123/ee94448943795093bffe to your computer and use it in GitHub Desktop.
Save CGA1123/ee94448943795093bffe to your computer and use it in GitHub Desktop.
100 Doors Problem in SML
(* 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