Advertisement
Guest User

Untitled

a guest
Mar 27th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.48 KB | None | 0 0
  1. // Learn more about F# at http://fsharp.org
  2. // See the 'F# Tutorial' project for more help.
  3. type instruction =
  4.     | ADD
  5.     | SUB
  6.     | MUL
  7.     | SQR
  8.     | DIV
  9.     | PUSH of int
  10.  
  11. type stack = int list
  12.  
  13. exception BadExpression of (instruction * stack)
  14.  
  15. let intInstr (x, y) =
  16.     match x, y with
  17.     | ADD, a::b::ys -> (b + a) :: ys : stack
  18.     | SUB, a::b::ys -> (b - a) :: ys
  19.     | MUL, a::b::ys -> (b * a) :: ys
  20.     | SQR, a::ys -> (a * a) :: ys
  21.     | DIV, a::b::ys -> try (b / a) :: ys with
  22.                        | :? System.DivideByZeroException -> printf "HI"; ys
  23.     | PUSH x, ys -> x :: ys
  24.     | _, _ -> raise (BadExpression (x, y))
  25.  
  26. let wypiszInstr = function
  27.     | ( PUSH x ) -> " PUSH " + System.Convert.ToString(x)
  28.     | ( SQR ) -> " SQR "
  29.     | ( ADD ) -> " ADD "
  30.     | ( SUB ) -> " SUB "
  31.     | ( MUL ) -> " MUL "
  32.  
  33. exception ZlyStos of (instruction list)
  34.  
  35. let intpProg (is) =
  36.     let rec iPS = function
  37.             | ([], x::xs) -> x
  38.             | (i::is, xs) -> try iPS(is, intInstr(i, xs)) with BadExpression (x, y) ->
  39.                                                                (
  40.                                                                    printfn "Pozostale elementy stosu: %A" is
  41.                                                                    1
  42.                                                                )
  43.     iPS(is, [])
  44.  
  45. [<EntryPoint>]
  46. let main argv =
  47.     printfn "%A" <| intpProg ([DIV])
  48.  
  49.     0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement