Guest User

Untitled

a guest
Jul 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1.  
  2. (* 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).
  3. I should write a function interpret(formula, dect) that returns a unary predicte that evaluates the formal *)
  4.  
  5. I have tried something like this :-/
  6.  
  7.  
  8. let pred =
  9. [ "a", true;
  10. "b", true;
  11. "c", false; ]
  12. // |> Map.ofList;; (* Convert list to Map *)
  13.  
  14. let pred = [("a", true); ("b", true); ("c", false)]
  15.  
  16. let formelinterprter (??? ) =
  17. match x with
  18. | :? AND -> //delete x y then append "x and y"
  19. | :? OR -> // delete x y then append "x or y"
  20. | :? NOT -> // delete x then append "x not"
  21. | _ -> printfn "The input is something else"
  22.  
  23.  
  24.  
  25. I found a solution in Python:
  26.  
  27.  
  28. # 11. interpret
  29. lang = {'a': True,
  30. 'b': True,
  31. 'c': False
  32. }
  33. def interpret(formula, d):
  34. stack = []
  35. for w in formula.split():
  36. if w == 'AND':
  37. x,y = (stack.pop(), stack.pop())
  38. stack.append(x and y)
  39. elif w == 'OR':
  40. x,y = (stack.pop(), stack.pop())
  41. stack.append(x or y)
  42. elif w == 'NOT':
  43. x = stack.pop()
  44. stack.append(not x)
  45. else:
  46. stack.append(d[w])
  47. print w, stack
  48. return stack[0]
  49.  
  50. print interpret('a b AND c NOT AND', lang)
Add Comment
Please, Sign In to add comment