Created
April 20, 2013 06:57
-
-
Save majiang/5425087 to your computer and use it in GitHub Desktop.
F#談話室 (4) で書いたコード
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
type Expression = | |
| Add of Expression * Expression | |
| Mul of Expression * Expression | |
| Val of int | |
| Var of string | |
let rec parenth = function | |
| Add (x, y) -> "(" + (tostring x) + "+" + (tostring y) + ")" | |
| x -> tostring x | |
and tostring = function | |
| Add (x, y) -> (tostring x) + "+" + (tostring y) | |
| Mul (x, y) -> (parenth x) + "*" + (parenth y) | |
| Val x -> x.ToString() | |
| Var x -> x | |
let rec substitute x y = function | |
| Add (l, r) -> Add (substitute x y l, substitute x y r) | |
| Mul (l, r) -> Mul (substitute x y l, substitute x y r) | |
| Val z -> Val z | |
| Var z -> if x = z then Val y else Var z | |
let rec eval = function | |
| Add (l, r) -> (eval l) + (eval r) | |
| Mul (l, r) -> (eval l) * (eval r) | |
| Val z -> z | |
| Var z -> raise (System.ArgumentOutOfRangeException("cannot evaluate formula with variables.")) | |
let rec hasMulAdd = function | |
| Val z -> false | |
| Var z -> false | |
| Add (l, r) -> (hasMulAdd l) || (hasMulAdd r) | |
| Mul (Add (x, y), z) -> true | |
| Mul (x, Add (y, z)) -> true | |
| Mul (l, r) -> (hasMulAdd l) || (hasMulAdd r) | |
let rec expand = function | |
| Add (l, r) -> Add (expand l, expand r) | |
| Val z -> Val z | |
| Var z -> Var z | |
| Mul (Add (a, b), c) -> | |
if hasMulAdd a || hasMulAdd b || hasMulAdd c then | |
Mul (Add (expand a, expand b), expand c) | |
else | |
Add (Mul (a, c), Mul(b, c)) | |
| Mul (a, Add (b, c)) -> | |
if hasMulAdd a || hasMulAdd b || hasMulAdd c then | |
Mul (expand a, Add (expand b, expand c)) | |
else | |
Add (Mul (a, b), Mul(a, c)) | |
| Mul (p, q) -> Mul (p, q) | |
//let rec deepexpand = function | |
// | Mul (p, q) -> Mul (expand p, expand q) | |
// | x -> expand x | |
let printFormula name expression = | |
System.Console.WriteLine("{0} = {1}", name, tostring expression) | |
let main = | |
let showexpands f0 = | |
printFormula "f0" f0 | |
printFormula "f1" (f0 |> expand) | |
printFormula "f2" (f0 |> expand |> expand) | |
printFormula "f3" (f0 |> expand |> expand |> expand) | |
printFormula "f4" (f0 |> expand |> expand |> expand |> expand) | |
printFormula "f5" (f0 |> expand |> expand |> expand |> expand |> expand) | |
showexpands (Mul (Mul (Add (Var "x", Val 1), Add (Var "x", Val 2)), Add (Var "x", Val 3))) | |
showexpands (Mul (Add (Var "x", Val 1), Mul(Add (Var "x", Val 2), Add (Var "x", Val 3)))) | |
main |
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
f0 = (x+1)*(x+2)*(x+3) | |
f1 = (x*(x+2)+1*(x+2))*(x+3) | |
f2 = (x*x+x*2+1*x+1*2)*(x+3) | |
f3 = (x*x+x*2)*(x+3)+(1*x+1*2)*(x+3) | |
f4 = x*x*(x+3)+x*2*(x+3)+1*x*(x+3)+1*2*(x+3) | |
f5 = x*x*x+x*x*3+x*2*x+x*2*3+1*x*x+1*x*3+1*2*x+1*2*3 | |
f0 = (x+1)*(x+2)*(x+3) | |
f1 = (x+1)*(x*(x+3)+2*(x+3)) | |
f2 = (x+1)*(x*x+x*3+2*x+2*3) | |
f3 = x*(x*x+x*3+2*x+2*3)+1*(x*x+x*3+2*x+2*3) | |
f4 = x*(x*x+x*3)+x*(2*x+2*3)+1*(x*x+x*3)+1*(2*x+2*3) | |
f5 = x*x*x+x*x*3+x*2*x+x*2*3+1*x*x+1*x*3+1*2*x+1*2*3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment