Skip to content

Instantly share code, notes, and snippets.

@sshine
Created October 7, 2013 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sshine/401a9b092fc0833e9d98 to your computer and use it in GitHub Desktop.
Save sshine/401a9b092fc0833e9d98 to your computer and use it in GitHub Desktop.
(* Recursive definition *)
fun max3 (x, y, z) = Int.max(x, Int.max(y, z))
fun min3 (x, y, z) = Int.min(x, Int.min(y, z))
fun findMax 0 = 0
| findMax 1 = 1
| findMax 2 = 2
| findMax 3 = 0
| findMax 4 = 1
| findMax n = max3 (findMin (n-5), findMin (n-4), findMin (n-3))
and findMin 0 = 0
| findMin 1 = 1
| findMin 2 = 2
| findMin 3 = 0
| findMin 4 = 0
| findMin n = min3 (findMax (n-5), findMax (n-4), findMax (n-3))
(* Closed form *)
fun findMax 0 = 0
| findMax 1 = 1
| findMax 2 = 2
| findMax n =
case (n mod 8) of
3 => 0
| 4 => 1
| 5 => 2
| 6 => 2
| 7 => 2
| 0 => 0
| 1 => 0
| 2 => 0
| _ => raise Fail "(n mod 8) > 7?"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment