Guest User

Untitled

a guest
Dec 3rd, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. exception NotEnaughOp of code;;
  2. raise (NotEnaughOp s)
  3.  
  4. type code = Elem of bool | Not | And | Or | Xor;;
  5.  
  6. let eval listOfOper =
  7.  
  8. let getVal l x =
  9. if x = Not then not (List.hd l)
  10. else if (List.tl l = []) then raise (NotEnaughOp x)
  11. else (
  12. match x with
  13. | And -> (List.hd l) && (List.hd (List.tl l))
  14. | Or -> (List.hd l) || (List.hd (List.tl l))
  15. | Xor -> ((List.hd l) && not (List.hd (List.tl l))) || (not (List.hd l) && (List.hd (List.tl l)))
  16. )
  17. in
  18.  
  19. let rec evalH (listOfOper, acc, currVal) =
  20. match listOfOper with
  21. [] -> List.hd acc
  22. | (x :: xs) -> if x = Elem true then evalH(xs, true :: acc, true)
  23. else if x = Elem false then evalH(xs, false :: acc, false)
  24.  
  25. else evalH(xs, [(getVal acc x)], (getVal acc x))
  26. in
  27.  
  28. evalH (listOfOper, [], false) ;;
Add Comment
Please, Sign In to add comment