Created
May 14, 2015 14:15
-
-
Save Glorp/509d89b8b38456ba3e9e to your computer and use it in GitHub Desktop.
1-9 and things
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
datatype exp = Num of int | |
| Plus of exp * int | |
| Minus of exp * int | |
fun eval (Num n) = n | |
| eval (Plus (a, b)) = eval a + b | |
| eval (Minus (a, b)) = eval a - b | |
fun expstr (Num n) = Int.toString n | |
| expstr (Plus (a, b)) = expstr a ^ "+" ^ Int.toString b | |
| expstr (Minus (a, b)) = expstr a ^ "-" ^ Int.toString b | |
fun join (n, (Num x)) = Num ((x * 10) + n) | |
| join (n, (Plus (a, b))) = Plus (a, (b * 10) + n) | |
| join (n, (Minus (a, b))) = Minus (a, (b * 10) + n) | |
fun next (exp, n) = | |
let val p = Plus (exp, n) | |
val m = Minus (exp, n) | |
val j = join (n, exp) | |
fun foo x = (x, n + 1) | |
in [foo p, foo m, foo j] | |
end | |
val res = | |
let fun generate (exp, n) = | |
if n = 10 | |
then if eval exp = 100 then [exp] else [] | |
else List.concat (map generate (next (exp, n))) | |
in generate (Num 1, 2) | |
end | |
val resstr = map expstr res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment