Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Project $safeprojectname$
- //Created by Björn Dagerman 2015/05 (dagerman@kth.se)
- //
- //TestModule: A module with a simple function for demo purposes
- namespace Demo
- open System.Text.RegularExpressions
- module TestModule =
- let printList list = if list <> [] then printfn "%A" list
- type GenTree<'data> =
- |Leaf of 'data
- |SubTree of GenTree<'data> * 'data * GenTree<'data>
- type Calc =
- |Operator of (float->float->float)
- |Operand of float
- let IsOperator (str:string) =
- match str with
- | "+" | "-" | "x" | "/" -> true
- |_ -> false
- let parseToFloat (str:string) =
- System.Single.Parse str
- let parseAsString (str:string list) =
- let StringOfExpressions = System.String.Concat(List.toSeq str)
- let delimiters = "(\+|x|\/|\-)"
- Regex.Split (StringOfExpressions, delimiters)
- let ParseToOperator (str:string) =
- match str with
- |"+" -> (+)
- |"-" -> (-)
- |"x" -> (*)
- |"/" -> (/)
- |_ -> failwith "Not valid operator"
- let BuildTree (t1, t2, t3) =
- SubTree(t1,t2,t3)
- let CheckFirstExpression (list:string list) =
- match list with
- |x::x2::x3::xs when (not (IsOperator x)) && (IsOperator x2) && (not (IsOperator x3)) -> BuildTree(Leaf (Operand (float x)), (Operator (ParseToOperator x2)), Leaf (Operand (float x3)))
- |_ -> failwith "This expression is incomplete"
- let rec eval tree =
- match tree with
- |Leaf (Operand x) -> x
- |SubTree(tl,oper,tr) -> match oper with
- |Operator (+) -> (eval tl) + (eval tr)
- |Operator (-) -> (eval tl) - (eval tr)
- |Operator (*) -> (eval tl) * (eval tr)
- |Operator (/) -> (eval tl) / (eval tr)
- |_-> failwith "wrong operator"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement