Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Learn more about F# at http://fsharp.org
- // See the 'F# Tutorial' project for more help.
- type instruction =
- | ADD
- | SUB
- | MUL
- | SQR
- | DIV
- | PUSH of int
- type stack = int list
- exception BadExpression of (instruction * stack)
- let intInstr (x, y) =
- match x, y with
- | ADD, a::b::ys -> (b + a) :: ys : stack
- | SUB, a::b::ys -> (b - a) :: ys
- | MUL, a::b::ys -> (b * a) :: ys
- | SQR, a::ys -> (a * a) :: ys
- | DIV, a::b::ys -> try (b / a) :: ys with
- | :? System.DivideByZeroException -> printf "HI"; ys
- | PUSH x, ys -> x :: ys
- | _, _ -> raise (BadExpression (x, y))
- let wypiszInstr = function
- | ( PUSH x ) -> " PUSH " + System.Convert.ToString(x)
- | ( SQR ) -> " SQR "
- | ( ADD ) -> " ADD "
- | ( SUB ) -> " SUB "
- | ( MUL ) -> " MUL "
- exception ZlyStos of (instruction list)
- let intpProg (is) =
- let rec iPS = function
- | ([], x::xs) -> x
- | (i::is, xs) -> try iPS(is, intInstr(i, xs)) with BadExpression (x, y) ->
- (
- printfn "Pozostale elementy stosu: %A" is
- 1
- )
- iPS(is, [])
- [<EntryPoint>]
- let main argv =
- printfn "%A" <| intpProg ([DIV])
- 0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement