0 people like it.

equationParseExample.fsx

Here's a very short, very simple, example for Algebraic Manipulation of the equation x + y = z

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
//Example solver for x + y = z

//Input parameters
type Equation =
   { X: int option;
     Y: int option;
     Z: int option; }

//Input values
let equation =
  { X = None;
    Y = None;
    Z = None; }

//Each manipulation of x + y = z with all cases for too much or not enough provided information provided
let solveUnknown eq =
    match eq.X with 
    | Some x ->
      match eq.Y with
      | Some y ->
        match eq.Z with
        | Some z -> printfn "Too much info was provided"
        | None -> printfn "x + y = %d" (x + y)
      | None -> 
        match eq.Z with
        | Some z ->
          printfn "z - x = %d" (z - x)
        | None -> printfn "only x(%d) was provided" x
    | None -> 
      match eq.Y with
      | Some y -> 
        match eq.Z with
        | Some z -> 
          printfn "z - y = %d" (z - y)
        | None -> printfn " only y(%d) was provided" y
      | None ->
        match eq.Z with
        | Some z -> printfn "only z(%d) was provided" z
        | None -> printfn "no info was given";;

//Execute the equation
solveUnknown equation
Equation.X: int option
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
type 'T option = Option<'T>

Full name: Microsoft.FSharp.Core.option<_>
Equation.Y: int option
Equation.Z: int option
val equation : Equation

Full name: Script.equation
union case Option.None: Option<'T>
val solveUnknown : eq:Equation -> unit

Full name: Script.solveUnknown
val eq : Equation
union case Option.Some: Value: 'T -> Option<'T>
val x : int
val y : int
val z : int
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Raw view Test code New version

More information

Link:http://fssnip.net/kp
Posted:11 years ago
Author:Virgil
Tags: #algebra #algebraicmanipulation #algebraic , tryfsharp