Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 4.95 KB | None | 0 0
  1. module Assignment3
  2.  
  3. type 'a BinTree =
  4.      Leaf
  5.    | Node of 'a * 'a BinTree * 'a BinTree
  6.  
  7. // 3.1
  8. let rec inOrder bTree =
  9.     match bTree with
  10.     | Leaf -> []
  11.     | Node(v,left,right) -> (inOrder left) @ [v] @ (inOrder right)
  12.  
  13. // 3.2
  14. let rec mapInOrder f bTree =
  15.     match bTree with
  16.     | Leaf -> Leaf
  17.     | Node(v,left,right) -> Node(f v, (mapInOrder f left), (mapInOrder f right))
  18.  
  19. // 3.3
  20. let rec foldInOrder f e bTree =
  21.     match bTree with
  22.     | Leaf -> e
  23.     | Node(v,left,right) -> f v (foldInOrder f left right)
  24.  
  25. // 3.4, 3.5, 3.6
  26. let update x v s = Map.add x v s
  27.  
  28. type aExp =
  29.     | N of int
  30.     | V of string
  31.     | Add of aExp * aExp
  32.     | Mul of aExp * aExp
  33.     | Sub of aExp * aExp
  34.     | Inc of string
  35.  
  36. let rec A a s =
  37.     match a with
  38.     | N n         -> (n,s)
  39.     | V x         -> (Map.find x s, s)
  40.     | Add(a1, a2) -> let (x,_) = A a1 s
  41.                      let (y,_) = A a2 s
  42.                      (x+y,s)
  43.     | Mul(a1, a2) -> let (x,_) = A a1 s
  44.                      let (y,_) = A a2 s
  45.                      (x*y,s)
  46.     | Sub(a1, a2) -> let (x,_) = A a1 s
  47.                      let (y,_) = A a2 s
  48.                      (x-y,s)
  49.     | Inc x       -> let n = Map.find x s in (n+1, update x (n+1) s)
  50.  
  51. type bExp =
  52.     | TT
  53.     | FF
  54.     | Eq of aExp * aExp
  55.     | Lt of aExp * aExp
  56.     | Neg of bExp
  57.     | Con of bExp * bExp
  58.  
  59. let rec B b s =
  60.     match b with
  61.     | TT -> true
  62.     | FF -> false
  63.     | Eq (a1, a2) -> a1 = a2
  64.     | Lt (a1, a2) -> a1 < a2
  65.     | Neg b -> not (B b s)
  66.     | Con (b1, b2) -> (B b1 s) && (B b2 s)
  67.  
  68.  
  69. type stm =
  70.     | Ass of string * aExp
  71.     | Skip
  72.     | Seq of stm * stm
  73.     | ITE of bExp * stm * stm
  74.     | While of bExp * stm
  75.     | IF of bExp * stm
  76.     | RU of bExp * stm
  77.     | Inc of string
  78.  
  79. let rec I stm s =
  80.     match stm with
  81.     | Ass(x,a) -> update x a s
  82.     | Skip -> s
  83.     | Seq(stm1,stm2) -> I stm2 (I stm1 s)
  84.     | ITE(b,stm1,stm2) -> if B b s then I stm1 s else I stm2 s
  85.     | While(b,stm) -> if B b s then I stm s else s
  86.     | IF(b,stm) -> I (ITE(b,stm,Skip)) s
  87.     | RU(b,stm) -> I (Seq(stm,While(Neg b, stm))) s
  88.     | Inc(x) -> I (Ass(x, Add(V x, N 1))) s
  89.  
  90. // 3.4 - examples
  91.  
  92. let ex1 = Add(Sub(N 45, N 11), N 22)
  93.  
  94. let ex2 = Add(Mul(N 10, N 32),Sub(N 15, N 10))
  95.  
  96. let ex3 = Mul(N 33, Add(N 33, N 2))
  97.  
  98. let ex4 = Add(ex1, ex2)
  99.  
  100. let ex5 = Add(ex4,Sub(N 100, N 33))
  101.  
  102. // 3.7
  103.  
  104. type Fexpr =
  105.     | Const of float
  106.     | X
  107.     | Add of Fexpr * Fexpr
  108.     | Sub of Fexpr * Fexpr
  109.     | Mul of Fexpr * Fexpr
  110.     | Div of Fexpr * Fexpr
  111.     | Sin of Fexpr
  112.     | Cos of Fexpr
  113.     | Log of Fexpr
  114.     | Exp of Fexpr
  115.  
  116. let rec postfix fe =
  117.     match fe with
  118.     | Const c -> c.ToString(System.Globalization.CultureInfo.InvariantCulture)
  119.     | X -> X.ToString()
  120.     | Add(x,y) -> (postfix x) + " " + (postfix y) + " +"
  121.     | Sub(x,y) -> (postfix x) + " " + (postfix y) + " -"
  122.     | Mul(x,y) -> (postfix x) + " " + (postfix y) + " *"
  123.     | Div(x,y) -> (postfix x) + " " + (postfix y) + " /"
  124.     | Sin(x) -> (postfix x) + "sin "
  125.     | Cos(x) -> (postfix x) + "cos "
  126.     | Log(x) -> (postfix x) + "log "
  127.     | Exp(x) -> (postfix x) + "exp "
  128.  
  129.  
  130. // 3.8
  131. type Instruction =
  132.     | ADD | SUB | MULT | DIV | SIN
  133.     | COS | LOG | EXP  | PUSH of float
  134.  
  135. // 3.8 part 1
  136. type Stack =
  137.     | Empty
  138.     | Stack of float * Stack
  139.  
  140.  
  141. let intpInstr st inst =
  142.     match st with
  143.     | Empty ->
  144.         match inst with
  145.         | PUSH f -> Stack(f, Empty)
  146.         | _ -> st
  147.     | Stack(x,Empty) ->
  148.         match inst with
  149.         | PUSH f -> Stack(f, st)
  150.         | SIN    -> Stack(System.Math.Sin(x), Empty)
  151.         | COS    -> Stack(System.Math.Cos(x), Empty)
  152.         | LOG    -> Stack(System.Math.Log(x), Empty)
  153.         | EXP    -> Stack(System.Math.Exp(x), Empty)
  154.         | _ -> st
  155.     | Stack(x,Stack(y,s)) ->
  156.         match inst with
  157.         | PUSH f -> Stack(f,st)
  158.         | SIN    -> Stack(System.Math.Sin(x),Stack(y,s))
  159.         | COS    -> Stack(System.Math.Cos(x),Stack(y,s))
  160.         | LOG    -> Stack(System.Math.Log(x),Stack(y,s))
  161.         | EXP    -> Stack(System.Math.Exp(x),Stack(y,s))
  162.         | ADD    -> Stack((y + x), s)
  163.         | SUB    -> Stack((y - x), s)
  164.         | MULT   -> Stack((y * x), s)
  165.         | DIV    -> Stack((y / x), s)
  166.  
  167. // 3.8 part 2
  168.  
  169. let intpProg instSet = function
  170.         (Stack(f,_)) ->
  171.             List.foldBack (fun inst stack -> intpInstr stack inst) instSet Empty
  172.        
  173.  
  174. // 3.8 part 3
  175. let rec trans (fe, x) =
  176.     match fe with
  177.     | Const c -> [PUSH c]
  178.     | X -> trans(X, x)
  179.     | Add(a,b) -> ADD  :: (trans (b, x)) @ (trans (a, x))
  180.     | Sub(a,b) -> SUB  :: (trans (b, x)) @ (trans (a, x))
  181.     | Mul(a,b) -> MULT :: (trans (b, x)) @ (trans (a, x))
  182.     | Div(a,b) -> DIV  :: (trans (b, x)) @ (trans (a, x))
  183.     | Sin(a)   -> SIN  :: (trans (a, x))
  184.     | Cos(a)   -> COS  :: (trans (a, x))
  185.     | Log(a)   -> LOG  :: (trans (a, x))
  186.     | Exp(a)   -> EXP  :: (trans (a, x))
  187.  
  188. // 3.9
  189. (*
  190.  See the file ComplexNumbers.fs
  191. *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement