Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* Formalinterpeter: I have Unäry Predikates for example: "a", "b", "c". I have a Formal Postfixform with Operators NOT, AND and OR. For example: "a AND b OR NOT c" (Infix) gives: "a b AND c NOT OR" (Postfix).
- I should write a function interpret(formula, dect) that returns a unary predicte that evaluates the formal *)
- I have tried something like this :-/
- let pred =
- [ "a", true;
- "b", true;
- "c", false; ]
- // |> Map.ofList;; (* Convert list to Map *)
- let pred = [("a", true); ("b", true); ("c", false)]
- let formelinterprter (??? ) =
- match x with
- | :? AND -> //delete x y then append "x and y"
- | :? OR -> // delete x y then append "x or y"
- | :? NOT -> // delete x then append "x not"
- | _ -> printfn "The input is something else"
- I found a solution in Python:
- # 11. interpret
- lang = {'a': True,
- 'b': True,
- 'c': False
- }
- def interpret(formula, d):
- stack = []
- for w in formula.split():
- if w == 'AND':
- x,y = (stack.pop(), stack.pop())
- stack.append(x and y)
- elif w == 'OR':
- x,y = (stack.pop(), stack.pop())
- stack.append(x or y)
- elif w == 'NOT':
- x = stack.pop()
- stack.append(not x)
- else:
- stack.append(d[w])
- print w, stack
- return stack[0]
- print interpret('a b AND c NOT AND', lang)
Add Comment
Please, Sign In to add comment